是否可以使用带有Ruby 1.8的EventMachine启动多个并行的http请求

em-synchrony.rb用Fibers实现了这个function,但是我会选择带有1.8 MRI的非光纤版本。

EM.run do http = EM::Protocols::HttpClient2.connect("www.google.com", 80) request = http.get("/") request.callback do puts request.status EM.stop end end 

看一下em-http-request :

 EM.run do http1 = EventMachine::HttpRequest.new('http://example.com/1').get http1.callback do p http1.response end http2 = EventMachine::HttpRequest.new('http://example.com/2').get http2.callback do p http2.response end end 

如果您可以在EventMachine之外查看, Typhoeus是一个易于使用的HTTP客户端,它与Hydra一起提供,它可以并行处理多个请求。

我用它做了几件事,很容易设置。 这是我前几天写的一些未经测试的代码:

 require 'typhoeus' hydra = Typhoeus::Hydra.new(:max_concurrency => 10) urls.each do |url| request = Typhoeus::Request.new(url) request.on_complete do |resp| filename = resp.request.url.split('/').last puts "writing #{filename}" File.open(filename, 'w') do |fo| fo.write resp.body end end puts "queuing #{ url }" hydra.queue(request) end hydra.run