Skip to content

Instantly share code, notes, and snippets.

@headius
Created May 19, 2011 21:58
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save headius/981883 to your computer and use it in GitHub Desktop.
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
Copy link

DAddYE commented May 20, 2011

w00t

@headius
Copy link
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