Skip to content

Instantly share code, notes, and snippets.

@headius headius/output
Created May 19, 2011

Embed
What would you like to do?
Rubinius's actor API under JRuby
~/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
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
@DAddYE

This comment has been minimized.

Copy link

DAddYE commented May 20, 2011

w00t

@headius

This comment has been minimized.

Copy link
Owner Author

headius commented May 20, 2011

Indeed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.