|
1 | 1 | ### Simple asynchronous task
|
2 | 2 |
|
3 | 3 | future = Concurrent.future { sleep 0.1; 1 + 1 } # evaluation starts immediately
|
4 |
| - # => <#Concurrent::Edge::Future:0x7fad9ca186e0 pending blocks:[]> |
| 4 | + # => <#Concurrent::Edge::Future:0x7f9f549266e8 pending blocks:[]> |
5 | 5 | future.completed? # => false
|
6 | 6 | # block until evaluated
|
7 | 7 | future.value # => 2
|
|
11 | 11 | ### Failing asynchronous task
|
12 | 12 |
|
13 | 13 | future = Concurrent.future { raise 'Boom' }
|
14 |
| - # => <#Concurrent::Edge::Future:0x7fad9c9f95b0 pending blocks:[]> |
| 14 | + # => <#Concurrent::Edge::Future:0x7f9f548dd3f8 pending blocks:[]> |
15 | 15 | future.value # => nil
|
16 | 16 | future.value! rescue $! # => #<RuntimeError: Boom>
|
17 | 17 | future.reason # => #<RuntimeError: Boom>
|
|
23 | 23 |
|
24 | 24 | head = Concurrent.future { 1 }
|
25 | 25 | branch1 = head.then(&:succ)
|
26 |
| -branch2 = head.then(&:succ).then(&:succ) |
| 26 | +branch2 = head.then(&:succ).then(&:succ) |
| 27 | +# zipping futures |
27 | 28 | branch1.zip(branch2).value # => [2, 3]
|
28 |
| -(branch1 & branch2).then { |(a, b)| a + b }.value |
29 |
| - # => nil |
| 29 | +(branch1 & branch2).then { |a, b| a + b }.value! # => 5 |
| 30 | +(branch1 & branch2).then(&:+).value! # => 5 |
| 31 | +Concurrent.zip(branch1, branch2, branch1).then { |*values| values.reduce &:+ }.value! |
| 32 | + # => 7 |
30 | 33 | # pick only first completed
|
31 | 34 | (branch1 | branch2).value # => 2
|
32 | 35 |
|
|
45 | 48 |
|
46 | 49 | # will not evaluate until asked by #value or other method requiring completion
|
47 | 50 | future = Concurrent.delay { 'lazy' }
|
48 |
| - # => <#Concurrent::Edge::Future:0x7fad9c8fb3e8 pending blocks:[]> |
| 51 | + # => <#Concurrent::Edge::Future:0x7f9f53a57b58 pending blocks:[]> |
49 | 52 | sleep 0.1
|
50 | 53 | future.completed? # => false
|
51 | 54 | future.value # => "lazy"
|
52 | 55 |
|
53 | 56 | # propagates trough chain allowing whole or partial lazy chains
|
54 | 57 |
|
55 | 58 | head = Concurrent.delay { 1 }
|
56 |
| - # => <#Concurrent::Edge::Future:0x7fad9b158bf0 pending blocks:[]> |
| 59 | + # => <#Concurrent::Edge::Future:0x7f9f53a35508 pending blocks:[]> |
57 | 60 | branch1 = head.then(&:succ)
|
58 |
| - # => <#Concurrent::Edge::Future:0x7fad9b149ba0 pending blocks:[]> |
| 61 | + # => <#Concurrent::Edge::Future:0x7f9f53a17328 pending blocks:[]> |
59 | 62 | branch2 = head.delay.then(&:succ)
|
60 |
| - # => <#Concurrent::Edge::Future:0x7fad9b12a020 pending blocks:[]> |
| 63 | + # => <#Concurrent::Edge::Future:0x7f9f5590d868 pending blocks:[]> |
61 | 64 | join = branch1 & branch2
|
62 |
| - # => <#Concurrent::Edge::ArrayFuture:0x7fad9b8a0778 pending blocks:[]> |
| 65 | + # => <#Concurrent::Edge::ArrayFuture:0x7f9f558fd7d8 pending blocks:[]> |
63 | 66 |
|
64 | 67 | sleep 0.1 # nothing will complete # => 0
|
65 | 68 | [head, branch1, branch2, join].map(&:completed?) # => [false, false, false, false]
|
|
87 | 90 | ### Schedule
|
88 | 91 |
|
89 | 92 | scheduled = Concurrent.schedule(0.1) { 1 }
|
90 |
| - # => <#Concurrent::Edge::Future:0x7fad9a941e08 pending blocks:[]> |
| 93 | + # => <#Concurrent::Edge::Future:0x7f9f5581ed30 pending blocks:[]> |
91 | 94 |
|
92 | 95 | scheduled.completed? # => false
|
93 | 96 | scheduled.value # available after 0.1sec # => 1
|
94 | 97 |
|
95 | 98 | # and in chain
|
96 | 99 | scheduled = Concurrent.delay { 1 }.schedule(0.1).then(&:succ)
|
97 |
| - # => <#Concurrent::Edge::Future:0x7fad9b0aa7d0 pending blocks:[]> |
| 100 | + # => <#Concurrent::Edge::Future:0x7f9f548494c8 pending blocks:[]> |
98 | 101 | # will not be scheduled until value is requested
|
99 | 102 | sleep 0.1
|
100 | 103 | scheduled.value # returns after another 0.1sec # => 2
|
|
103 | 106 | ### Completable Future and Event
|
104 | 107 |
|
105 | 108 | future = Concurrent.future
|
106 |
| - # => <#Concurrent::Edge::CompletableFuture:0x7fad9a87b6e0 pending blocks:[]> |
| 109 | + # => <#Concurrent::Edge::CompletableFuture:0x7f9f5385bcf0 pending blocks:[]> |
107 | 110 | event = Concurrent.event
|
108 |
| - # => <#Concurrent::Edge::CompletableEvent:0x7fad9a86ba88 pending blocks:[]> |
| 111 | + # => <#Concurrent::Edge::CompletableEvent:0x7f9f53858ca8 pending blocks:[]> |
109 | 112 |
|
110 | 113 | # will be blocked until completed
|
111 | 114 | t1 = Thread.new { future.value }
|
112 | 115 | t2 = Thread.new { event.wait }
|
113 | 116 |
|
114 | 117 | future.success 1
|
115 |
| - # => <#Concurrent::Edge::CompletableFuture:0x7fad9a87b6e0 success blocks:[]> |
| 118 | + # => <#Concurrent::Edge::CompletableFuture:0x7f9f5385bcf0 success blocks:[]> |
116 | 119 | future.success 1 rescue $!
|
117 | 120 | # => #<Concurrent::MultipleAssignmentError: multiple assignment>
|
118 | 121 | future.try_success 2 # => false
|
119 | 122 | event.complete
|
120 |
| - # => <#Concurrent::Edge::CompletableEvent:0x7fad9a86ba88 completed blocks:[]> |
| 123 | + # => <#Concurrent::Edge::CompletableEvent:0x7f9f53858ca8 completed blocks:[]> |
121 | 124 |
|
122 | 125 | [t1, t2].each &:join
|
123 | 126 |
|
124 | 127 |
|
125 | 128 | ### Callbacks
|
126 | 129 |
|
127 |
| -queue = Queue.new # => #<Thread::Queue:0x007fad9a862320> |
| 130 | +queue = Queue.new # => #<Thread::Queue:0x007f9f53861b50> |
128 | 131 | future = Concurrent.delay { 1 + 1 }
|
129 |
| - # => <#Concurrent::Edge::Future:0x7fad9a853960 pending blocks:[]> |
| 132 | + # => <#Concurrent::Edge::Future:0x7f9f54853d10 pending blocks:[]> |
130 | 133 |
|
131 | 134 | future.on_success { queue << 1 } # evaluated asynchronously
|
132 |
| - # => <#Concurrent::Edge::Future:0x7fad9a853960 pending blocks:[]> |
| 135 | + # => <#Concurrent::Edge::Future:0x7f9f54853d10 pending blocks:[]> |
133 | 136 | future.on_success! { queue << 2 } # evaluated on completing thread
|
134 |
| - # => <#Concurrent::Edge::Future:0x7fad9a853960 pending blocks:[]> |
| 137 | + # => <#Concurrent::Edge::Future:0x7f9f54853d10 pending blocks:[]> |
135 | 138 |
|
136 | 139 | queue.empty? # => true
|
137 | 140 | future.value # => 2
|
|
142 | 145 | ### Thread-pools
|
143 | 146 |
|
144 | 147 | Concurrent.future(:fast) { 2 }.then(:io) { File.read __FILE__ }.wait
|
145 |
| - # => <#Concurrent::Edge::Future:0x7fad9a883958 success blocks:[]> |
| 148 | + # => <#Concurrent::Edge::Future:0x7f9f539545f8 success blocks:[]> |
146 | 149 |
|
147 | 150 |
|
148 | 151 | ### Interoperability with actors
|
|
163 | 166 |
|
164 | 167 | ### Interoperability with channels
|
165 | 168 |
|
166 |
| -ch1 = Concurrent::Edge::Channel.new # => #<Concurrent::Edge::Channel:0x007fad9c892ac8> |
167 |
| -ch2 = Concurrent::Edge::Channel.new # => #<Concurrent::Edge::Channel:0x007fad9c8904a8> |
| 169 | +ch1 = Concurrent::Edge::Channel.new # => #<Concurrent::Edge::Channel:0x007f9f558e5db8> |
| 170 | +ch2 = Concurrent::Edge::Channel.new # => #<Concurrent::Edge::Channel:0x007f9f558e4f80> |
168 | 171 |
|
169 | 172 | result = Concurrent.select(ch1, ch2)
|
170 |
| - # => <#Concurrent::Edge::CompletableFuture:0x7fad9b86aa88 pending blocks:[]> |
| 173 | + # => <#Concurrent::Edge::CompletableFuture:0x7f9f53a4fe58 pending blocks:[]> |
171 | 174 | ch1.push 1 # => nil
|
172 | 175 | result.value!
|
173 |
| - # => [1, #<Concurrent::Edge::Channel:0x007fad9c892ac8>] |
| 176 | + # => [1, #<Concurrent::Edge::Channel:0x007f9f558e5db8>] |
174 | 177 |
|
175 | 178 | Concurrent.
|
176 | 179 | future { 1+1 }.
|
177 | 180 | then_push(ch1)
|
178 |
| - # => <#Concurrent::Edge::Future:0x7fad9c898d88 pending blocks:[]> |
| 181 | + # => <#Concurrent::Edge::Future:0x7f9f53a2e758 pending blocks:[]> |
179 | 182 | result = Concurrent.
|
180 | 183 | future { '%02d' }.
|
181 | 184 | then_select(ch1, ch2).
|
182 | 185 | then { |format, (value, channel)| format format, value }
|
183 |
| - # => <#Concurrent::Edge::Future:0x7fad9b88b4e0 pending blocks:[]> |
| 186 | + # => <#Concurrent::Edge::Future:0x7f9f55925f30 pending blocks:[]> |
184 | 187 | result.value! # => "02"
|
185 | 188 |
|
186 | 189 |
|
187 | 190 | ### Common use-cases Examples
|
188 | 191 |
|
189 | 192 | # simple background processing
|
190 | 193 | Concurrent.future { do_stuff }
|
191 |
| - # => <#Concurrent::Edge::Future:0x7fad9b151b98 pending blocks:[]> |
| 194 | + # => <#Concurrent::Edge::Future:0x7f9f53a93e50 pending blocks:[]> |
192 | 195 |
|
193 | 196 | # parallel background processing
|
194 | 197 | jobs = 10.times.map { |i| Concurrent.future { i } }
|
|
200 | 203 |
|
201 | 204 | def schedule_job
|
202 | 205 | Concurrent.schedule(1) { do_stuff }.
|
203 |
| - rescue { |e| report_error e }. |
| 206 | + rescue { |e| StandardError === e ? report_error(e) : raise(e) }. |
204 | 207 | then { schedule_job unless @end }
|
205 | 208 | end # => :schedule_job
|
206 | 209 |
|
207 | 210 | schedule_job
|
208 |
| - # => <#Concurrent::Edge::Future:0x7fad9c96a6a8 pending blocks:[]> |
| 211 | + # => <#Concurrent::Edge::Future:0x7f9f548b01a0 pending blocks:[]> |
209 | 212 | @end = true # => true
|
210 | 213 |
|
211 | 214 |
|
@@ -247,7 +250,7 @@ def work(message)
|
247 | 250 | end
|
248 | 251 | end # => :work
|
249 | 252 |
|
250 |
| -data = Array.new(10) { |i| '*' * i } |
| 253 | +data = Array.new(10) { |i| '*' * i } |
251 | 254 | # => ["", "*", "**", "***", "****", "*****", "******", "*******", "********", "*********"]
|
252 | 255 | pool_size = 5 # => 5
|
253 | 256 |
|
|
0 commit comments