Skip to content

Instantly share code, notes, and snippets.

@fantactuka
Created October 28, 2012 20:51
Show Gist options
  • Save fantactuka/3969868 to your computer and use it in GitHub Desktop.
Save fantactuka/3969868 to your computer and use it in GitHub Desktop.
Parallel requests in sinatra
require "sinatra"
require "sinatra/async"
require "em-synchrony"
require "em-synchrony/em-http"
class App < Sinatra::Base
register Sinatra::Async
aget "/" do
Fiber.new {
urls = %w(http://bbc.com http://google.com)
results = EM::Synchrony::Iterator.new(urls, 2).map do |url, iter|
http = EventMachine::HttpRequest.new(url).aget
http.callback { iter.return(http) }
http.errback { iter.return(http) }
end
body { results.map(&:state).inspect }
}.resume
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment