Skip to content

Commit 5ce8761

Browse files
committed
Doc update
1 parent a5a3024 commit 5ce8761

32 files changed

+9513
-1445
lines changed

docs-source/channel.out.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ log
210210
# "producer 0 pushing 3",
211211
# "producer 1 pushing 1",
212212
# "producer 1 pushing 2",
213-
# "consumer 1 got 1. payload 3 from producer 0",
214-
# "consumer 0 got 1. payload 1 from producer 1",
215-
# "consumer 3 got 1. payload 2 from producer 1",
213+
# "consumer 0 got 1. payload 3 from producer 0",
216214
# "producer 1 pushing 3",
217-
# "consumer 2 got 1. payload 3 from producer 1"]
215+
# "consumer 2 got 1. payload 1 from producer 1",
216+
# "consumer 1 got 1. payload 2 from producer 1",
217+
# "consumer 3 got 1. payload 3 from producer 1"]
218218
```
219219

220220
The producers are much faster than consumers
@@ -279,9 +279,9 @@ log
279279
# "producer 1 pushing 2",
280280
# "producer 0 pushing 3",
281281
# "producer 1 pushing 3",
282+
# "consumer 2 got 1. payload 3 from producer 0",
282283
# "consumer 0 got 1. payload 2 from producer 0",
283284
# "consumer 1 got 1. payload 2 from producer 1",
284-
# "consumer 2 got 1. payload 3 from producer 0",
285285
# "consumer 3 got 1. payload 3 from producer 1"]
286286
```
287287

docs-source/erlang_actor.out.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,34 @@ actor.tell :done
4343
actor.terminated.value! # => 12
4444
```
4545

46+
### Receiving
47+
48+
Simplest message receive.
49+
50+
```ruby
51+
actor = Concurrent::ErlangActor.spawn(:on_thread) { receive }
52+
# => #<Concurrent::ErlangActor::Pid:0x000004>
53+
actor.tell :m
54+
# => #<Concurrent::ErlangActor::Pid:0x000004>
55+
actor.terminated.value! # => :m
56+
```
57+
58+
which also works for actor on pool,
59+
because if no block is given it will use a default block `{ |v| v }`
60+
61+
```ruby
62+
actor = Concurrent::ErlangActor.spawn(:on_pool) { receive { |v| v } }
63+
# => #<Concurrent::ErlangActor::Pid:0x000005>
64+
# can simply be following
65+
actor = Concurrent::ErlangActor.spawn(:on_pool) { receive }
66+
# => #<Concurrent::ErlangActor::Pid:0x000006>
67+
actor.tell :m
68+
# => #<Concurrent::ErlangActor::Pid:0x000006>
69+
actor.terminated.value! # => :m
70+
```
71+
72+
TBA
73+
4674
### Actor types
4775

4876
There are two types of actors.
@@ -65,7 +93,7 @@ Let's have a look at how the bodies of actors differ between the types:
6593

6694
```ruby
6795
ping = Concurrent::ErlangActor.spawn(:on_thread) { reply receive }
68-
# => #<Concurrent::ErlangActor::Pid:0x000004>
96+
# => #<Concurrent::ErlangActor::Pid:0x000007>
6997
ping.ask 42 # => 42
7098
```
7199

@@ -79,7 +107,7 @@ after the message is received has to be provided.
79107

80108
```ruby
81109
ping = Concurrent::ErlangActor.spawn(:on_pool) { receive { |m| reply m } }
82-
# => #<Concurrent::ErlangActor::Pid:0x000005>
110+
# => #<Concurrent::ErlangActor::Pid:0x000008>
83111
ping.ask 42 # => 42
84112
```
85113

@@ -116,16 +144,17 @@ actor = Concurrent::ErlangActor.spawn(:on_thread) do
116144
trap # equivalent of process_flag(trap_exit, true)
117145
receive
118146
end
119-
# => #<Concurrent::ErlangActor::Pid:0x000006>
147+
# => #<Concurrent::ErlangActor::Pid:0x000009>
120148
actor.terminated.value!
121-
# => #<Concurrent::ErlangActor::Exit:0x000007
122-
# @from=#<Concurrent::ErlangActor::Pid:0x000008>,
149+
# => #<Concurrent::ErlangActor::Exit:0x00000a
150+
# @from=#<Concurrent::ErlangActor::Pid:0x00000b>,
123151
# @link_terminated=true,
124152
# @reason=:err>
125153
```
126154

127155
### TODO
128156

157+
* receives
129158
* More erlang behaviour examples
130159
* Back pressure with bounded mailbox
131160
* _op methods

docs-source/promises.out.md

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ message.
713713
actor = Concurrent::ProcessingActor.act(an_argument = 2) do |actor, number|
714714
number ** 3
715715
end
716-
# => #<Concurrent::ProcessingActor:0x000021 ... termination:pending>
716+
# => #<Concurrent::ProcessingActor:0x000021 termination: pending>
717717
actor.termination.value! # => 8
718718
```
719719
Let's receive some messages though.
@@ -725,12 +725,12 @@ add_2_messages = Concurrent::ProcessingActor.act do |actor|
725725
a + b
726726
end
727727
end
728-
# => #<Concurrent::ProcessingActor:0x000024 ... termination:pending>
728+
# => #<Concurrent::ProcessingActor:0x000022 termination: pending>
729729
add_2_messages.tell_op 1
730-
# => #<Concurrent::Promises::Future:0x000027 pending>
730+
# => #<Concurrent::Promises::Future:0x000023 pending>
731731
add_2_messages.termination.resolved? # => false
732732
add_2_messages.tell_op 3
733-
# => #<Concurrent::Promises::Future:0x000028 pending>
733+
# => #<Concurrent::Promises::Future:0x000024 pending>
734734
add_2_messages.termination.value! # => 4
735735
```
736736

@@ -750,13 +750,13 @@ slow_counter = -> (actor, count) do
750750
end
751751
end
752752
end
753-
# => #<Proc:0x000029@promises.in.md:638 (lambda)>
753+
# => #<Proc:0x000025@promises.in.md:638 (lambda)>
754754

755755
actor = Concurrent::ProcessingActor.act_listening(
756756
Concurrent::Promises::Channel.new(2),
757757
0,
758758
&slow_counter)
759-
# => #<Concurrent::ProcessingActor:0x00002a ... termination:pending>
759+
# => #<Concurrent::ProcessingActor:0x000026 termination: pending>
760760
```
761761

762762
Now we can create a producer which will push messages only when there is a
@@ -777,10 +777,10 @@ produce = -> receiver, i do
777777
# do not continue
778778
end
779779
end
780-
# => #<Proc:0x00002d@promises.in.md:662 (lambda)>
780+
# => #<Proc:0x000027@promises.in.md:662 (lambda)>
781781

782782
Concurrent::Promises.future(actor, 0, &produce).run.wait!
783-
# => #<Concurrent::Promises::Future:0x00002e fulfilled with #<Concurrent::ProcessingActor:0x00002a @Mailbox=#<Concurrent::Promises::Channel:0x00002b capacity taken 2 of 2>, @Terminated=#<Concurrent::Promises::ResolvableFuture:0x00002c pending> termination:pending>>
783+
# => #<Concurrent::Promises::Future:0x000028 fulfilled with #<Concurrent::ProcessingActor:0x000026 termination: pending>>
784784

785785
actor.termination.value! # => 45
786786
```
@@ -792,17 +792,17 @@ actor.termination.value! # => 45
792792

793793
```ruby
794794
Concurrent::Promises.future { do_stuff }
795-
# => #<Concurrent::Promises::Future:0x00002f pending>
795+
# => #<Concurrent::Promises::Future:0x000029 pending>
796796
```
797797

798798
## Parallel background processing
799799

800800
```ruby
801801
tasks = 4.times.map { |i| Concurrent::Promises.future(i) { |i| i*2 } }
802-
# => [#<Concurrent::Promises::Future:0x000030 pending>,
803-
# #<Concurrent::Promises::Future:0x000031 pending>,
804-
# #<Concurrent::Promises::Future:0x000032 pending>,
805-
# #<Concurrent::Promises::Future:0x000033 pending>]
802+
# => [#<Concurrent::Promises::Future:0x00002a pending>,
803+
# #<Concurrent::Promises::Future:0x00002b pending>,
804+
# #<Concurrent::Promises::Future:0x00002c pending>,
805+
# #<Concurrent::Promises::Future:0x00002d pending>]
806806
Concurrent::Promises.zip(*tasks).value!
807807
# => [0, 2, 4, 6]
808808
```
@@ -856,11 +856,11 @@ Create the computer actor and send it 3 jobs.
856856

857857
```ruby
858858
computer = Concurrent::Actor.spawn Computer, :computer
859-
# => #<Concurrent::Actor::Reference:0x000034 /computer (Computer)>
859+
# => #<Concurrent::Actor::Reference:0x00002e /computer (Computer)>
860860
results = 3.times.map { computer.ask [:run, -> { sleep 0.01; :result }] }
861-
# => [#<Concurrent::Promises::Future:0x000035 pending>,
862-
# #<Concurrent::Promises::Future:0x000036 pending>,
863-
# #<Concurrent::Promises::Future:0x000037 pending>]
861+
# => [#<Concurrent::Promises::Future:0x00002f pending>,
862+
# #<Concurrent::Promises::Future:0x000030 pending>,
863+
# #<Concurrent::Promises::Future:0x000031 pending>]
864864
computer.ask(:status).value! # => {:running_jobs=>3}
865865
results.map(&:value!) # => [:result, :result, :result]
866866
```
@@ -892,7 +892,7 @@ body = lambda do |v|
892892
new_v
893893
end
894894
end
895-
# => #<Proc:0x000038@promises.in.md:765 (lambda)>
895+
# => #<Proc:0x000032@promises.in.md:765 (lambda)>
896896
Concurrent::Promises.future(0, &body).run.value! # => 5
897897
```
898898

@@ -923,7 +923,7 @@ DB = Concurrent::Actor::Utils::AdHoc.spawn :db, data do |data|
923923
data[message]
924924
end
925925
end
926-
# => #<Concurrent::Actor::Reference:0x000039 /db (Concurrent::Actor::Utils::AdHoc)>
926+
# => #<Concurrent::Actor::Reference:0x000033 /db (Concurrent::Actor::Utils::AdHoc)>
927927

928928
concurrent_jobs = 11.times.map do |v|
929929
DB.
@@ -955,7 +955,7 @@ DB_POOL = Concurrent::Actor::Utils::Pool.spawn!('DB-pool', pool_size) do |index|
955955
end
956956
end
957957
end
958-
# => #<Concurrent::Actor::Reference:0x00003a /DB-pool (Concurrent::Actor::Utils::Pool)>
958+
# => #<Concurrent::Actor::Reference:0x000034 /DB-pool (Concurrent::Actor::Utils::Pool)>
959959

960960
concurrent_jobs = 11.times.map do |v|
961961
DB_POOL.
@@ -992,7 +992,7 @@ DB_INTERNAL_POOL = Concurrent::Array.new data
992992
# "*********"]
993993

994994
max_tree = Concurrent::Throttle.new 3
995-
# => #<Concurrent::Throttle:0x00003b capacity available 3 of 3>
995+
# => #<Concurrent::Throttle:0x000035 capacity available 3 of 3>
996996

997997
futures = 11.times.map do |i|
998998
max_tree.
@@ -1017,9 +1017,9 @@ buffer and how to apply backpressure to slow down the queries.
10171017
require 'json'
10181018

10191019
channel = Concurrent::Promises::Channel.new 6
1020-
# => #<Concurrent::Promises::Channel:0x00003c capacity taken 0 of 6>
1020+
# => #<Concurrent::Promises::Channel:0x000036 capacity taken 0 of 6>
10211021
cancellation, origin = Concurrent::Cancellation.new
1022-
# => #<Concurrent::Cancellation:0x00003d pending>
1022+
# => #<Concurrent::Cancellation:0x000037 pending>
10231023

10241024
def query_random_text(cancellation, channel)
10251025
Concurrent::Promises.future do
@@ -1045,7 +1045,7 @@ end # => :query_random_text
10451045

10461046
words = [] # => []
10471047
words_throttle = Concurrent::Throttle.new 1
1048-
# => #<Concurrent::Throttle:0x00003e capacity available 1 of 1>
1048+
# => #<Concurrent::Throttle:0x000038 capacity available 1 of 1>
10491049

10501050
def count_words_in_random_text(cancellation, channel, words, words_throttle)
10511051
channel.pop_op.then do |response|
@@ -1067,16 +1067,16 @@ end # => :count_words_in_random_text
10671067
query_processes = 3.times.map do
10681068
Concurrent::Promises.future(cancellation, channel, &method(:query_random_text)).run
10691069
end
1070-
# => [#<Concurrent::Promises::Future:0x00003f pending>,
1071-
# #<Concurrent::Promises::Future:0x000040 pending>,
1072-
# #<Concurrent::Promises::Future:0x000041 pending>]
1070+
# => [#<Concurrent::Promises::Future:0x000039 pending>,
1071+
# #<Concurrent::Promises::Future:0x00003a pending>,
1072+
# #<Concurrent::Promises::Future:0x00003b pending>]
10731073

10741074
word_counter_processes = 2.times.map do
10751075
Concurrent::Promises.future(cancellation, channel, words, words_throttle,
10761076
&method(:count_words_in_random_text)).run
10771077
end
1078-
# => [#<Concurrent::Promises::Future:0x000042 pending>,
1079-
# #<Concurrent::Promises::Future:0x000043 pending>]
1078+
# => [#<Concurrent::Promises::Future:0x00003c pending>,
1079+
# #<Concurrent::Promises::Future:0x00003d pending>]
10801080

10811081
sleep 0.05
10821082
```
@@ -1086,14 +1086,14 @@ Let it run for a while, then cancel it, and ensure that the runs were all fulfil
10861086

10871087
```ruby
10881088
origin.resolve
1089-
# => #<Concurrent::Promises::ResolvableEvent:0x000044 resolved>
1089+
# => #<Concurrent::Promises::ResolvableEvent:0x00003e resolved>
10901090
query_processes.map(&:wait!)
1091-
# => [#<Concurrent::Promises::Future:0x00003f fulfilled with nil>,
1092-
# #<Concurrent::Promises::Future:0x000040 fulfilled with nil>,
1093-
# #<Concurrent::Promises::Future:0x000041 fulfilled with nil>]
1091+
# => [#<Concurrent::Promises::Future:0x000039 fulfilled with nil>,
1092+
# #<Concurrent::Promises::Future:0x00003a fulfilled with nil>,
1093+
# #<Concurrent::Promises::Future:0x00003b fulfilled with nil>]
10941094
word_counter_processes.map(&:wait!)
1095-
# => [#<Concurrent::Promises::Future:0x000042 fulfilled with nil>,
1096-
# #<Concurrent::Promises::Future:0x000043 fulfilled with nil>]
1095+
# => [#<Concurrent::Promises::Future:0x00003c fulfilled with nil>,
1096+
# #<Concurrent::Promises::Future:0x00003d fulfilled with nil>]
10971097
words # => [7, 7, 7, 7]
10981098
```
10991099

@@ -1114,24 +1114,24 @@ repeating_scheduled_task = -> interval, cancellation, task do
11141114
# Alternatively use chain to schedule always.
11151115
then { repeating_scheduled_task.call(interval, cancellation, task) }
11161116
end
1117-
# => #<Proc:0x000045@promises.in.md:951 (lambda)>
1117+
# => #<Proc:0x00003f@promises.in.md:951 (lambda)>
11181118

11191119
cancellation, origin = Concurrent::Cancellation.new
1120-
# => #<Concurrent::Cancellation:0x000046 pending>
1120+
# => #<Concurrent::Cancellation:0x000040 pending>
11211121

11221122
task = -> cancellation do
11231123
5.times do
11241124
cancellation.check!
11251125
do_stuff
11261126
end
11271127
end
1128-
# => #<Proc:0x000047@promises.in.md:962 (lambda)>
1128+
# => #<Proc:0x000041@promises.in.md:962 (lambda)>
11291129

11301130
result = Concurrent::Promises.future(0.1, cancellation, task, &repeating_scheduled_task).run
1131-
# => #<Concurrent::Promises::Future:0x000048 pending>
1131+
# => #<Concurrent::Promises::Future:0x000042 pending>
11321132
sleep 0.03
11331133
origin.resolve
1134-
# => #<Concurrent::Promises::ResolvableEvent:0x000049 resolved>
1134+
# => #<Concurrent::Promises::ResolvableEvent:0x000043 resolved>
11351135
result.result
11361136
# => [false,
11371137
# nil,

0 commit comments

Comments
 (0)