Skip to content

Commit 810a591

Browse files
committed
Add IO example
1 parent b898a47 commit 810a591

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

doc/actor/io.in.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'concurrent'
2+
3+
# logger = Logger.new(STDOUT)
4+
# Concurrent.configuration.logger = logger.method(:add)
5+
6+
# First option is to use operation pool
7+
8+
class ActorDoingIO < Concurrent::Actor::RestartingContext
9+
def on_message(message)
10+
# do IO operation
11+
end
12+
13+
def default_executor
14+
Concurrent.configuration.global_operation_pool
15+
end
16+
end #
17+
18+
actor_doing_io = ActorDoingIO.spawn :actor_doing_io
19+
actor_doing_io.executor == Concurrent.configuration.global_operation_pool
20+
21+
# It can be also built into a pool so there is not too many IO operations
22+
23+
class IOWorker < Concurrent::Actor::Utils::AbstractWorker
24+
def work(io_job)
25+
# do IO work
26+
sleep 1
27+
puts "#{path} second:#{Time.now.to_i} message:#{io_job}"
28+
end
29+
30+
def default_executor
31+
Concurrent.configuration.global_operation_pool
32+
end
33+
end #
34+
35+
pool = Concurrent::Actor::Utils::Pool.spawn('pool', 2) do |balancer, index|
36+
IOWorker.spawn(name: "worker-#{index}", supervise: true, args: [balancer])
37+
end
38+
39+
pool << 1 << 2 << 3 << 4 << 5 << 6
40+
41+
# prints two lines each second
42+
# /pool/worker-0 second:1414677666 message:1
43+
# /pool/worker-1 second:1414677666 message:2
44+
# /pool/worker-0 second:1414677667 message:3
45+
# /pool/worker-1 second:1414677667 message:4
46+
# /pool/worker-0 second:1414677668 message:5
47+
# /pool/worker-1 second:1414677668 message:6
48+
49+
sleep 4

doc/actor/io.out.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
require 'concurrent' # => true
2+
3+
# logger = Logger.new(STDOUT)
4+
# Concurrent.configuration.logger = logger.method(:add)
5+
6+
# First option is to use operation pool
7+
8+
class ActorDoingIO < Concurrent::Actor::RestartingContext
9+
def on_message(message)
10+
# do IO operation
11+
end
12+
13+
def default_executor
14+
Concurrent.configuration.global_operation_pool
15+
end
16+
end
17+
18+
actor_doing_io = ActorDoingIO.spawn :actor_doing_io
19+
# => #<Concurrent::Actor::Reference /actor_doing_io (ActorDoingIO)>
20+
actor_doing_io.executor == Concurrent.configuration.global_operation_pool
21+
# => true
22+
23+
# It can be also built into a pool so there is not too many IO operations
24+
25+
class IOWorker < Concurrent::Actor::Utils::AbstractWorker
26+
def work(io_job)
27+
# do IO work
28+
sleep 1
29+
puts "#{path} second:#{Time.now.to_i} message:#{io_job}"
30+
end
31+
32+
def default_executor
33+
Concurrent.configuration.global_operation_pool
34+
end
35+
end
36+
37+
pool = Concurrent::Actor::Utils::Pool.spawn('pool', 2) do |balancer, index|
38+
IOWorker.spawn(name: "worker-#{index}", supervise: true, args: [balancer])
39+
end
40+
# => #<Concurrent::Actor::Reference /pool (Concurrent::Actor::Utils::Pool)>
41+
42+
pool << 1 << 2 << 3 << 4 << 5 << 6
43+
# => #<Concurrent::Actor::Reference /pool (Concurrent::Actor::Utils::Pool)>
44+
45+
# prints two lines each second
46+
# /pool/worker-0 second:1414677666 message:1
47+
# /pool/worker-1 second:1414677666 message:2
48+
# /pool/worker-0 second:1414677667 message:3
49+
# /pool/worker-1 second:1414677667 message:4
50+
# /pool/worker-0 second:1414677668 message:5
51+
# /pool/worker-1 second:1414677668 message:6
52+
53+
sleep 4 # => 4

doc/actor/main.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ Blocking operations could starve the `default_task_pool`. However there are two
9696
(which is intended for blocking operations) sending results back to self in messages.
9797
- Create an actor using `global_operation_pool` instead of `global_task_pool`, e.g.
9898
`AnIOActor.spawn name: :blocking, executor: Concurrent.configuration.global_operation_pool`.
99+
100+
### Example
101+
102+
{include:file:doc/actor/io.out.rb}
99103

100104
## Dead letter routing
101105

lib/concurrent/actor.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
module Concurrent
88
# TODO https://github.com/celluloid/celluloid/wiki/Supervision-Groups ?
99
# TODO Remote actors using DRb
10-
# TODO IO interoperation
1110
# TODO un/become
11+
# TODO supervision tree, pause children on error in parent, pause may need higher priority
12+
# TODO more effective executor
1213

1314
# {include:file:doc/actor/main.md}
1415
module Actor

0 commit comments

Comments
 (0)