Skip to content

Instantly share code, notes, and snippets.

@aflatter
Created August 7, 2008 12:15
Show Gist options
  • Save aflatter/4388 to your computer and use it in GitHub Desktop.
Save aflatter/4388 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'eventmachine'
class Handler
include EventMachine::Deferrable
def takes_long_time(seconds = 5)
sleep(seconds)
puts "Awoke after #{seconds} seconds."
succeed(seconds)
end
def self.takes_long_time(seconds)
obj = new
obj.takes_long_time(seconds)
obj.callback do |result|
puts "Sleeping #{result} seconds succeeded."
end
obj
end
end
EM.run do
results = []
a = Handler.takes_long_time(1)
b = Handler.takes_long_time(2)
[a, b].each do |obj|
obj.callback do |result|
results << result
if results.size == 2
puts "We're done"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment