em-synchrony.rb用Fibers实现了这个功能,但是我会选择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
答案 0 :(得分:4)
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
答案 1 :(得分:4)
如果您可以在EventMachine之外查看,Typhoeus是一个易于使用的Hydra附带的HTTP客户端,它可以并行处理多个请求。
我用它做了几件事,而且很容易设置。这是我前几天写的一些未经测试的代码:
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