em-synchrony.rb はこの機能をファイバーで実装していますが、私は 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-synchrony.rb はこの機能をファイバーで実装していますが、私は 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は 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