Rubinius's actor API under JRuby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/projects/jruby ➔ jruby rbx_actor_example.rb | |
Processing: 0 | |
Processing: 4 | |
Processing: 7 | |
Processing: 5 | |
Processing: 8 | |
Processing: 3 | |
Processing: 2 | |
Processing: 6 | |
Processing: 1 | |
Processing: 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubinius' | |
require 'rubinius/actor' | |
Ready = Struct.new(:this) | |
Work = Struct.new(:msg) | |
@supervisor = Actor.spawn do | |
supervisor = Actor.current | |
work_loop = Proc.new do | |
loop do | |
work = Actor.receive | |
puts("Processing: #{work.msg}") | |
supervisor << Ready[Actor.current] | |
end | |
end | |
Actor.trap_exit = true | |
ready_workers = [] | |
10.times do |x| | |
# start 10 worker actors | |
ready_workers << Actor.spawn_link(&work_loop) | |
end | |
loop do | |
Actor.receive do |f| | |
f.when(Ready) do |who| | |
# SNIP | |
end | |
f.when(Work) do |work| | |
ready_workers.pop << work | |
end | |
f.when(Actor::DeadActorError) do |exit| | |
print "Actor exited with message: #{exit.reason}\n" | |
ready_workers << Actor.spawn_link(&work_loop) | |
end | |
end | |
end | |
end | |
10.times do |idx| | |
@supervisor << Work[idx] | |
end | |
sleep 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
w00t