Created
April 19, 2011 02:43
-
-
Save mperham/926713 to your computer and use it in GitHub Desktop.
Simple ping/pong with Actors
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 '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