Skip to content

Commit 723a705

Browse files
committed
Doc updates
1 parent 780890b commit 723a705

32 files changed

+2594
-3320
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ be obeyed though. Features developed in `concurrent-ruby-edge` are expected to m
227227
* [Promises::Channel](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Promises/Channel.html)
228228
A first in first out channel that accepts messages with push family of methods and returns
229229
messages with pop family of methods.
230-
Pop and push operations can be represented as futures, see {#pop_op} and {#push_op}.
231-
The capacity of the channel can be limited to support back pressure, use capacity option in {#initialize}.
232-
{#pop} method blocks ans {#pop_op} returns pending future if there is no message in the channel.
233-
If the capacity is limited the {#push} method blocks and {#push_op} returns pending future.
230+
Pop and push operations can be represented as futures, see `#pop_op` and `#push_op`.
231+
The capacity of the channel can be limited to support back pressure, use capacity option in `#initialize`.
232+
`#pop` method blocks ans `#pop_op` returns pending future if there is no message in the channel.
233+
If the capacity is limited the `#push` method blocks and `#push_op` returns pending future.
234234
* [Cancellation](http://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/Cancellation.html)
235235
The Cancellation abstraction provides cooperative cancellation.
236236

docs-source/channel.in.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ one will be blocked until new messages is pushed.
3131
```ruby
3232
threads = Array.new(3) { |i| Thread.new { ch.pop } } #
3333
sleep 0.01 # let the threads run
34-
threads
34+
threads
3535
ch.push message: 3
3636
threads.map(&:value)
3737
```

docs-source/channel.out.md

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ threads = Array.new(3) { |i| Thread.new { ch.push message: i } }
1616
sleep 0.01 # let the threads run
1717
threads
1818
# => [#<Thread:[email protected]:14 dead>,
19-
# #<Thread:[email protected]:14 dead>,
20-
# #<Thread:[email protected]:14 sleep_forever>]
19+
# #<Thread:[email protected]:14 sleep_forever>,
20+
# #<Thread:[email protected]:14 dead>]
2121
```
2222

2323
When message is popped the last thread continues and finishes as well.
2424

2525
```ruby
26-
ch.pop # => {:message=>0}
26+
ch.pop # => {:message=>2}
2727
threads.map(&:join)
2828
# => [#<Thread:[email protected]:14 dead>,
2929
# #<Thread:[email protected]:14 dead>,
@@ -38,14 +38,11 @@ one will be blocked until new messages is pushed.
3838
```ruby
3939
threads = Array.new(3) { |i| Thread.new { ch.pop } }
4040
sleep 0.01 # let the threads run
41-
threads
42-
# => [#<Thread:[email protected]:32 dead>,
43-
# #<Thread:[email protected]:32 dead>,
44-
# #<Thread:[email protected]:32 sleep_forever>]
41+
threads.map(&:status) # => [false, false, "sleep"]
4542
ch.push message: 3
4643
# => #<Concurrent::Promises::Channel:0x000002 capacity taken 0 of 2>
4744
threads.map(&:value)
48-
# => [{:message=>1}, {:message=>2}, {:message=>3}]
45+
# => [{:message=>0}, {:message=>1}, {:message=>3}]
4946
```
5047

5148
### Promises integration
@@ -55,11 +52,11 @@ therefore all operations can be represented as futures.
5552

5653
```ruby
5754
ch = Concurrent::Promises::Channel.new 2
58-
# => #<Concurrent::Promises::Channel:0x000009 capacity taken 0 of 2>
55+
# => #<Concurrent::Promises::Channel:0x000006 capacity taken 0 of 2>
5956
push_operations = Array.new(3) { |i| ch.push_op message: i }
60-
# => [#<Concurrent::Promises::Future:0x00000a fulfilled with #<Concurrent::Promises::Channel:0x000009 capacity taken 2 of 2>>,
61-
# #<Concurrent::Promises::Future:0x00000b fulfilled with #<Concurrent::Promises::Channel:0x000009 capacity taken 2 of 2>>,
62-
# #<Concurrent::Promises::ResolvableFuture:0x00000c pending>]
57+
# => [#<Concurrent::Promises::Future:0x000007 fulfilled with #<Concurrent::Promises::Channel:0x000006 capacity taken 2 of 2>>,
58+
# #<Concurrent::Promises::Future:0x000008 fulfilled with #<Concurrent::Promises::Channel:0x000006 capacity taken 2 of 2>>,
59+
# #<Concurrent::Promises::ResolvableFuture:0x000009 pending>]
6360
```
6461

6562
> We do not have to sleep here letting the futures execute as Threads.
@@ -73,14 +70,14 @@ making a space for a new message.
7370
```ruby
7471
ch.pop_op.value! # => {:message=>0}
7572
push_operations.map(&:value!)
76-
# => [#<Concurrent::Promises::Channel:0x000009 capacity taken 2 of 2>,
77-
# #<Concurrent::Promises::Channel:0x000009 capacity taken 2 of 2>,
78-
# #<Concurrent::Promises::Channel:0x000009 capacity taken 2 of 2>]
73+
# => [#<Concurrent::Promises::Channel:0x000006 capacity taken 2 of 2>,
74+
# #<Concurrent::Promises::Channel:0x000006 capacity taken 2 of 2>,
75+
# #<Concurrent::Promises::Channel:0x000006 capacity taken 2 of 2>]
7976

8077
pop_operations = Array.new(3) { |i| ch.pop_op }
81-
# => [#<Concurrent::Promises::ResolvableFuture:0x00000d fulfilled with {:message=>1}>,
82-
# #<Concurrent::Promises::ResolvableFuture:0x00000e fulfilled with {:message=>2}>,
83-
# #<Concurrent::Promises::ResolvableFuture:0x00000f pending>]
78+
# => [#<Concurrent::Promises::ResolvableFuture:0x00000a fulfilled with {:message=>1}>,
79+
# #<Concurrent::Promises::ResolvableFuture:0x00000b fulfilled with {:message=>2}>,
80+
# #<Concurrent::Promises::ResolvableFuture:0x00000c pending>]
8481
ch.push message: 3 # (push|pop) can be freely mixed with (push_o|pop_op)
8582
pop_operations.map(&:value)
8683
# => [{:message=>1}, {:message=>2}, {:message=>3}]
@@ -94,21 +91,21 @@ returns a pair to be able to find out which channel had the message available.
9491

9592
```ruby
9693
ch1 = Concurrent::Promises::Channel.new 2
97-
# => #<Concurrent::Promises::Channel:0x000010 capacity taken 0 of 2>
94+
# => #<Concurrent::Promises::Channel:0x00000d capacity taken 0 of 2>
9895
ch2 = Concurrent::Promises::Channel.new 2
99-
# => #<Concurrent::Promises::Channel:0x000011 capacity taken 0 of 2>
96+
# => #<Concurrent::Promises::Channel:0x00000e capacity taken 0 of 2>
10097
ch1.push 1
101-
# => #<Concurrent::Promises::Channel:0x000010 capacity taken 1 of 2>
98+
# => #<Concurrent::Promises::Channel:0x00000d capacity taken 1 of 2>
10299
ch2.push 2
103-
# => #<Concurrent::Promises::Channel:0x000011 capacity taken 1 of 2>
100+
# => #<Concurrent::Promises::Channel:0x00000e capacity taken 1 of 2>
104101

105102
Concurrent::Promises::Channel.select([ch1, ch2])
106-
# => [#<Concurrent::Promises::Channel:0x000010 capacity taken 0 of 2>, 1]
103+
# => [#<Concurrent::Promises::Channel:0x00000d capacity taken 0 of 2>, 1]
107104
ch1.select(ch2)
108-
# => [#<Concurrent::Promises::Channel:0x000011 capacity taken 0 of 2>, 2]
105+
# => [#<Concurrent::Promises::Channel:0x00000e capacity taken 0 of 2>, 2]
109106

110107
Concurrent::Promises.future { 3 + 4 }.then_channel_push(ch1)
111-
# => #<Concurrent::Promises::Future:0x000012 pending>
108+
# => #<Concurrent::Promises::Future:0x00000f pending>
112109
Concurrent::Promises::Channel.
113110
# or `ch1.select_op(ch2)` would be equivalent
114111
select_op([ch1, ch2]).
@@ -125,7 +122,7 @@ They always return immediately and indicate either success or failure.
125122

126123
```ruby
127124
ch
128-
# => #<Concurrent::Promises::Channel:0x000009 capacity taken 0 of 2>
125+
# => #<Concurrent::Promises::Channel:0x000006 capacity taken 0 of 2>
129126
ch.try_push 1 # => true
130127
ch.try_push 2 # => true
131128
ch.try_push 3 # => false
@@ -142,7 +139,7 @@ when the timeout option is used.
142139

143140
```ruby
144141
ch
145-
# => #<Concurrent::Promises::Channel:0x000009 capacity taken 0 of 2>
142+
# => #<Concurrent::Promises::Channel:0x000006 capacity taken 0 of 2>
146143
ch.push 1, 0.01 # => true
147144
ch.push 2, 0.01 # => true
148145
ch.push 3, 0.01 # => false
@@ -159,7 +156,7 @@ if the consumers are not keeping up.
159156

160157
```ruby
161158
channel = Concurrent::Promises::Channel.new 2
162-
# => #<Concurrent::Promises::Channel:0x000013 capacity taken 0 of 2>
159+
# => #<Concurrent::Promises::Channel:0x000010 capacity taken 0 of 2>
163160
log = Concurrent::Array.new # => []
164161

165162
producers = Array.new 2 do |i|
@@ -170,8 +167,8 @@ producers = Array.new 2 do |i|
170167
end
171168
end
172169
end
173-
# => [#<Thread:0x000014@channel.in.md:133 run>,
174-
# #<Thread:0x000015@channel.in.md:133 run>]
170+
# => [#<Thread:0x000011@channel.in.md:133 run>,
171+
# #<Thread:0x000012@channel.in.md:133 run>]
175172

176173
consumers = Array.new 4 do |i|
177174
Thread.new(i) do |consumer|
@@ -183,22 +180,22 @@ consumers = Array.new 4 do |i|
183180
end
184181
end
185182
end
186-
# => [#<Thread:0x000016@channel.in.md:142 run>,
187-
# #<Thread:0x000017@channel.in.md:142 run>,
188-
# #<Thread:0x000018@channel.in.md:142 run>,
189-
# #<Thread:0x000019@channel.in.md:142 run>]
183+
# => [#<Thread:0x000013@channel.in.md:142 run>,
184+
# #<Thread:0x000014@channel.in.md:142 run>,
185+
# #<Thread:0x000015@channel.in.md:142 run>,
186+
# #<Thread:0x000016@channel.in.md:142 run>]
190187

191188
# wait for all to finish
192189
producers.map(&:join)
193-
# => [#<Thread:0x000014@channel.in.md:133 dead>,
194-
# #<Thread:0x000015@channel.in.md:133 dead>]
190+
# => [#<Thread:0x000011@channel.in.md:133 dead>,
191+
# #<Thread:0x000012@channel.in.md:133 dead>]
195192
consumers.map(&:join)
196-
# => [#<Thread:0x000016@channel.in.md:142 dead>,
197-
# #<Thread:0x000017@channel.in.md:142 dead>,
198-
# #<Thread:0x000018@channel.in.md:142 dead>,
199-
# #<Thread:0x000019@channel.in.md:142 dead>]
193+
# => [#<Thread:0x000013@channel.in.md:142 dead>,
194+
# #<Thread:0x000014@channel.in.md:142 dead>,
195+
# #<Thread:0x000015@channel.in.md:142 dead>,
196+
# #<Thread:0x000016@channel.in.md:142 dead>]
200197
# investigate log
201-
log
198+
log
202199
# => ["producer 0 pushing 0",
203200
# "producer 0 pushing 1",
204201
# "producer 0 pushing 2",
@@ -229,7 +226,7 @@ that run a thread pool.
229226

230227
```ruby
231228
channel = Concurrent::Promises::Channel.new 2
232-
# => #<Concurrent::Promises::Channel:0x00001a capacity taken 0 of 2>
229+
# => #<Concurrent::Promises::Channel:0x000017 capacity taken 0 of 2>
233230
log = Concurrent::Array.new # => []
234231

235232
def produce(channel, log, producer, i)
@@ -251,22 +248,22 @@ end # => :consume
251248
producers = Array.new 2 do |i|
252249
Concurrent::Promises.future(channel, log, i) { |*args| produce *args, 0 }.run
253250
end
254-
# => [#<Concurrent::Promises::Future:0x00001b pending>,
255-
# #<Concurrent::Promises::Future:0x00001c pending>]
251+
# => [#<Concurrent::Promises::Future:0x000018 pending>,
252+
# #<Concurrent::Promises::Future:0x000019 pending>]
256253

257254
consumers = Array.new 4 do |i|
258255
Concurrent::Promises.future(channel, log, i) { |*args| consume *args, 0 }.run
259256
end
260-
# => [#<Concurrent::Promises::Future:0x00001d pending>,
261-
# #<Concurrent::Promises::Future:0x00001e pending>,
262-
# #<Concurrent::Promises::Future:0x00001f pending>,
263-
# #<Concurrent::Promises::Future:0x000020 pending>]
257+
# => [#<Concurrent::Promises::Future:0x00001a pending>,
258+
# #<Concurrent::Promises::Future:0x00001b pending>,
259+
# #<Concurrent::Promises::Future:0x00001c pending>,
260+
# #<Concurrent::Promises::Future:0x00001d pending>]
264261

265262
# wait for all to finish
266263
producers.map(&:value!) # => [:done, :done]
267264
consumers.map(&:value!) # => [:done, :done, :done, :done]
268265
# investigate log
269-
log
266+
log
270267
# => ["producer 0 pushing 0",
271268
# "producer 1 pushing 0",
272269
# "consumer 1 got 0. payload 0 from producer 1",
@@ -295,19 +292,19 @@ The operations have to be paired to succeed.
295292

296293
```ruby
297294
channel = Concurrent::Promises::Channel.new 0
298-
# => #<Concurrent::Promises::Channel:0x000021 capacity taken 0 of 0>
295+
# => #<Concurrent::Promises::Channel:0x00001e capacity taken 0 of 0>
299296
thread = Thread.new { channel.pop }; sleep 0.01
300297
# allow the thread to go to sleep
301298
thread
302-
# => #<Thread:0x000022@channel.in.md:214 sleep_forever>
299+
# => #<Thread:0x00001f@channel.in.md:246 sleep_forever>
303300
# succeeds because there is matching pop operation waiting in the thread
304301
channel.try_push(:v1) # => true
305302
# remains pending, since there is no matching operation
306303
push = channel.push_op(:v2)
307-
# => #<Concurrent::Promises::ResolvableFuture:0x000023 pending>
304+
# => #<Concurrent::Promises::ResolvableFuture:0x000020 pending>
308305
thread.value # => :v1
309306
# the push operation resolves as a pairing pop is called
310307
channel.pop # => :v2
311308
push
312-
# => #<Concurrent::Promises::ResolvableFuture:0x000023 fulfilled with #<Concurrent::Promises::Channel:0x000021 capacity taken 0 of 0>>
309+
# => #<Concurrent::Promises::ResolvableFuture:0x000020 fulfilled with #<Concurrent::Promises::Channel:0x00001e capacity taken 0 of 0>>
313310
```

0 commit comments

Comments
 (0)