Skip to content

Commit a8fe298

Browse files
committed
internal actor events now support payload & minor improvements
1 parent 3bd2cf1 commit a8fe298

28 files changed

+187
-136
lines changed

doc/actor/define.in.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'concurrent'
2-
31
Message = Struct.new :action, :value #
42

53
class AnActor < Concurrent::Actor::RestartingContext

doc/actor/define.out.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'concurrent' # => true
2-
31
Message = Struct.new :action, :value
42

53
class AnActor < Concurrent::Actor::RestartingContext
@@ -35,5 +33,5 @@ def on_event(event)
3533
an_actor << :boo << Message.new(:add, 1)
3634
an_actor.ask!(Message.new(:value, nil)) # => 1
3735
an_actor << :terminate!
38-
# => #<Concurrent::Actor::Reference:0x7fd6451f78e0 /an_actor (AnActor)>
36+
# => #<Concurrent::Actor::Reference:0x7fb6fc9165d0 /an_actor (AnActor)>
3937

doc/actor/format.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
require 'pry'
44
require 'pp'
55

6+
root = File.dirname(File.expand_path(Process.argv0))
67
input_paths = if ARGV.empty?
7-
Dir.glob("#{File.dirname(__FILE__)}/*.in.rb")
8+
Dir.glob("#{root}/*.in.rb")
89
else
910
ARGV
1011
end.map { |p| File.expand_path p }
1112

1213
input_paths.each_with_index do |input_path, i|
1314

1415
pid = fork do
15-
require_relative 'init.rb'
16+
require File.join(root, 'init.rb')
1617

1718
begin
1819
output_path = input_path.gsub /\.in\.rb$/, '.out.rb'

doc/actor/init.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
require 'concurrent/actor'
1+
require 'concurrent'
2+
require 'concurrent-edge'

doc/actor/io.in.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def default_executor
2323
class IOWorker < Concurrent::Actor::Utils::AbstractWorker
2424
def work(io_job)
2525
# do IO work
26-
sleep 1
27-
puts "#{path} second:#{Time.now.to_i} message:#{io_job}"
26+
sleep 0.1
27+
puts "#{path} second:#{(Time.now.to_f*100).floor} message:#{io_job}"
2828
end
2929

3030
def default_executor
@@ -46,4 +46,4 @@ def default_executor
4646
# /pool/worker-0 second:1414677668 message:5
4747
# /pool/worker-1 second:1414677668 message:6
4848

49-
sleep 4
49+
sleep 1

doc/actor/io.out.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'concurrent' # => true
1+
require 'concurrent' # => false
22

33
# logger = Logger.new(STDOUT)
44
# Concurrent.configuration.logger = logger.method(:add)
@@ -16,7 +16,7 @@ def default_executor
1616
end
1717

1818
actor_doing_io = ActorDoingIO.spawn :actor_doing_io
19-
# => #<Concurrent::Actor::Reference:0x7fd6451fff40 /actor_doing_io (ActorDoingIO)>
19+
# => #<Concurrent::Actor::Reference:0x7fb6fc906068 /actor_doing_io (ActorDoingIO)>
2020
actor_doing_io.executor == Concurrent.configuration.global_operation_pool
2121
# => true
2222

@@ -25,8 +25,8 @@ def default_executor
2525
class IOWorker < Concurrent::Actor::Utils::AbstractWorker
2626
def work(io_job)
2727
# do IO work
28-
sleep 1
29-
puts "#{path} second:#{Time.now.to_i} message:#{io_job}"
28+
sleep 0.1
29+
puts "#{path} second:#{(Time.now.to_f*100).floor} message:#{io_job}"
3030
end
3131

3232
def default_executor
@@ -37,10 +37,10 @@ def default_executor
3737
pool = Concurrent::Actor::Utils::Pool.spawn('pool', 2) do |balancer, index|
3838
IOWorker.spawn(name: "worker-#{index}", args: [balancer])
3939
end
40-
# => #<Concurrent::Actor::Reference:0x7fd6451ecaf8 /pool (Concurrent::Actor::Utils::Pool)>
40+
# => #<Concurrent::Actor::Reference:0x7fb6fc964190 /pool (Concurrent::Actor::Utils::Pool)>
4141

4242
pool << 1 << 2 << 3 << 4 << 5 << 6
43-
# => #<Concurrent::Actor::Reference:0x7fd6451ecaf8 /pool (Concurrent::Actor::Utils::Pool)>
43+
# => #<Concurrent::Actor::Reference:0x7fb6fc964190 /pool (Concurrent::Actor::Utils::Pool)>
4444

4545
# prints two lines each second
4646
# /pool/worker-0 second:1414677666 message:1
@@ -50,4 +50,4 @@ def default_executor
5050
# /pool/worker-0 second:1414677668 message:5
5151
# /pool/worker-1 second:1414677668 message:6
5252

53-
sleep 4 # => 4
53+
sleep 1 # => 1

doc/actor/main.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ Any existing behavior can be subclassed
161161
By subclassing {Behaviour::Pausing} and overriding {Behaviour::Pausing#restart!}. Implementing
162162
{AbstractContext#on_event} could be also considered.
163163

164+
_We'll be happy to answer any other questions,
165+
just [open an Issue](https://github.com/ruby-concurrency/concurrent-ruby/issues/new) or find us on
166+
https://gitter.im/ruby-concurrency/concurrent-ruby._
167+
164168
## Speed
165169

166170
Simple benchmark Actor vs Celluloid, the numbers are looking good

doc/actor/messaging.in.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require 'concurrent'
21
require 'algebrick'
32

43
# Actor message protocol definition with Algebrick
@@ -17,14 +16,17 @@ def on_message(message)
1716
(on Add.(~any, ~any) do |a, b|
1817
a + b
1918
end),
20-
# or use multi-assignment
19+
# or using multi-assignment
2120
(on ~Subtract do |(a, b)|
2221
a - b
2322
end)
2423
end
2524
end #
2625

2726
calculator = Calculator.spawn('calculator')
28-
calculator.ask! Add[1, 2]
29-
calculator.ask! Subtract[1, 0.5]
30-
calculator << :terminate!
27+
addition = calculator.ask Add[1, 2]
28+
substraction = calculator.ask Subtract[1, 0.5]
29+
results = (addition & substraction)
30+
results.value!
31+
32+
calculator.ask! :terminate!

doc/actor/messaging.out.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
require 'concurrent' # => true
2-
require 'algebrick' # => true
1+
require 'algebrick' # => false
32

43
# Actor message protocol definition with Algebrick
54
Protocol = Algebrick.type do
@@ -17,16 +16,21 @@ def on_message(message)
1716
(on Add.(~any, ~any) do |a, b|
1817
a + b
1918
end),
20-
# or use multi-assignment
19+
# or using multi-assignment
2120
(on ~Subtract do |(a, b)|
2221
a - b
2322
end)
2423
end
2524
end
2625

2726
calculator = Calculator.spawn('calculator')
28-
# => #<Concurrent::Actor::Reference:0x7fb94a2565d8 /calculator (Calculator)>
29-
calculator.ask! Add[1, 2] # => 3
30-
calculator.ask! Subtract[1, 0.5] # => 0.5
31-
calculator << :terminate!
32-
# => #<Concurrent::Actor::Reference:0x7fb94a2565d8 /calculator (Calculator)>
27+
# => #<Concurrent::Actor::Reference:0x7fb6fc915ec8 /calculator (Calculator)>
28+
addition = calculator.ask Add[1, 2]
29+
# => <#Concurrent::Edge::Future:0x7fb6fc937190 pending blocks:[]>
30+
substraction = calculator.ask Subtract[1, 0.5]
31+
# => <#Concurrent::Edge::Future:0x7fb6fc935598 pending blocks:[]>
32+
results = (addition & substraction)
33+
# => <#Concurrent::Edge::ArrayFuture:0x7fb6fc967ea8 pending blocks:[]>
34+
results.value! # => [3, 0.5]
35+
36+
calculator.ask! :terminate! # => true

doc/actor/quick.out.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ def on_message(message)
1717
# `link: true` makes the actor linked to root actor and supervised
1818
# which is default behavior
1919
adder = Adder.spawn(name: :adder, link: true, args: [1])
20-
# => #<Concurrent::Actor::Reference:0x7fb94bb30040 /adder (Adder)>
20+
# => #<Concurrent::Actor::Reference:0x7fb6fc88ff58 /adder (Adder)>
2121
adder.parent
22-
# => #<Concurrent::Actor::Reference:0x7fd644229008 / (Concurrent::Actor::Root)>
22+
# => #<Concurrent::Actor::Reference:0x7fb6fe0f5db8 / (Concurrent::Actor::Root)>
2323

2424
# tell and forget
2525
adder.tell(:add).tell(:add)
26-
# => #<Concurrent::Actor::Reference:0x7fb94bb30040 /adder (Adder)>
26+
# => #<Concurrent::Actor::Reference:0x7fb6fc88ff58 /adder (Adder)>
2727
# ask to get result
2828
adder.ask!(:add) # => 4
2929
# fail the actor

0 commit comments

Comments
 (0)