Skip to content

Instantly share code, notes, and snippets.

@mperham
Created April 19, 2011 02:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mperham/926713 to your computer and use it in GitHub Desktop.
Save mperham/926713 to your computer and use it in GitHub Desktop.
Simple ping/pong with Actors
require 'actor'
# Execute with Rubinius: rbx ex1.rb
def error_loop(&block)
loop(&block)
rescue Exception => ex
puts ex.message
puts ex.backtrace.join("\n")
end
pong = nil
ping = Actor.spawn do
error_loop do
count = Actor.receive
print '.'
break puts(count) if count > 1000
pong << (count + 1)
end
end
pong = Actor.spawn do
error_loop do
count = Actor.receive
print '-'
break puts(count) if count > 1000
ping << (count + 1)
end
end
ping << 1
# Let the actors process while the main thread sleeps...
sleep 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment