@@ -8,7 +8,7 @@ Async uses tasks to represent units of concurrency. Those tasks are backed by fi
88
99``` ruby
1010Async do
11- # Internally non-blocking write:
11+ # Internally non-blocking write:
1212 puts " Hello World"
1313end
1414```
@@ -18,10 +18,10 @@ Conceptually, `Async{...}` means execute the given block of code sequentially, b
1818``` ruby
1919Async do |task |
2020 # Start two tasks that will run asynchronously:
21- child1 = Async {sleep 1 ; puts " Hello" }
21+ child1 = Async {sleep 1 ; puts " Hello" }
2222 # Using task.async is the same as Async, but is slightly more efficient:
2323 child2 = task.async{sleep 2 ; puts " World" }
24-
24+
2525 # Wait for both tasks to complete:
2626 child1.wait
2727 child2.wait
5252Sync do
5353 # This is a no-op, as it's already in an event loop:
5454 Sync {...}
55-
55+
5656 # It's semantically equivalent to:
5757 Async {...}.wait
5858 # but it is more efficient.
@@ -111,13 +111,13 @@ Barriers provide a way to manage an unbounded number of tasks.
111111``` ruby
112112Async do
113113 barrier = Async ::Barrier .new
114-
114+
115115 items.each do |item |
116116 barrier.async do
117117 process(item)
118118 end
119119 end
120-
120+
121121 # Process the tasks in order of completion:
122122 barrier.wait do |task |
123123 result = task.wait
@@ -126,7 +126,7 @@ Async do
126126 # If you don't want to wait for any more tasks you can break:
127127 break
128128 end
129-
129+
130130 # Or just wait for all tasks to finish:
131131 barrier.wait # May raise an exception if a task failed.
132132ensure
@@ -143,7 +143,7 @@ Semaphores allow you to limit the level of concurrency to a fixed number of task
143143Async do |task |
144144 barrier = Async ::Barrier .new
145145 semaphore = Async ::Semaphore .new (4 , parent: barrier)
146-
146+
147147 # Since the semaphore.async may block, we need to run the work scheduling in a child task:
148148 task.async do
149149 items.each do |item |
@@ -152,7 +152,7 @@ Async do |task|
152152 end
153153 end
154154 end
155-
155+
156156 # Wait for all the work to complete:
157157 barrier.wait
158158ensure
@@ -169,7 +169,7 @@ Idlers are like semaphores but with a limit defined by current processor utiliza
169169Async do
170170 # Create an idler that will aim for a load average of 80%:
171171 idler = Async ::Idler .new (0.8 )
172-
172+
173173 # Some list of work to be done:
174174 work.each do |work |
175175 idler.async do
@@ -189,16 +189,16 @@ Queues allow you to share data between disconnected tasks:
189189``` ruby
190190Async do |task |
191191 queue = Async ::Queue .new
192-
192+
193193 reader = task.async do
194194 while chunk = socket.gets
195195 queue.push(chunk)
196196 end
197-
197+ end
198198 # After this point, we won't be able to add items to the queue, and popping items will eventually result in nil once all items are dequeued:
199199 queue.close
200200 end
201-
201+
202202 # Process items from the queue:
203203 while line = queue.pop
204204 process(line)
@@ -211,7 +211,7 @@ The above program may have unbounded memory use, so it can be a good idea to use
211211``` ruby
212212Async do |task |
213213 queue = Async ::LimitedQueue .new (8 )
214-
214+
215215 # Everything else is the same from the queue example, except that the pushing onto the queue will block once 8 items are buffered.
216216end
217217```
0 commit comments