Above example has a race condition - you'll stop the eventloop as soon as one of the requests has finished. To address this, you can use the built in "Multi" interface:
EventMachine.run do
multi = EventMachine::MultiRequest.new
multi.add :google, EventMachine::HttpRequest.new('http://www.google.com/').get
multi.add :yahoo, EventMachine::HttpRequest.new('http://www.yahoo.com/').get
multi.callback do
puts multi.responses[:callback]
puts multi.responses[:errback]
EventMachine.stop
end
end
See em-http wiki page for more: https://github.com/igrigorik/em-http-request/wiki/Parallel-Requests#synchronizing-with-multi-interface