【发布时间】:2011-12-26 17:25:04
【问题描述】:
我使用 eventmachine 创建很多 http 查询。 Http 服务器可以执行这些连接。 但定期对 100 个查询调用 5-7 errback。 为什么会这样?
require 'rubygems'
require 'eventmachine'
require 'em-http'
urls = []
100.times do
urls << 'http://127.0.0.1/'
end
if urls.size < 1
puts "Usage: #{$0} <url> <url> <...>"
exit
end
pending = urls.size
EM.run do
urls.each do |url|
http = EM::HttpRequest.new(url).get
http.callback do
puts "#{url}\n#{http.response_header.status} - #{http.response.length} bytes\n"
pending -= 1
EM.stop if pending < 1
end
http.errback do
puts "E::#{url}\n" + http.error.to_s
pending -= 1
EM.stop if pending < 1
end
end
end
}
【问题讨论】:
-
什么样的错误?这可能与连接的另一端有关,例如,如果另一台服务器只能处理 50 个并发连接,则一旦超过,它可能会开始拒绝连接
-
仅供参考,如果您想异步执行多个 HTTP 请求,您可能需要查看 Typhoeus and its associated Hydra。
标签: ruby eventmachine