2011-12-26 75 views
0

我使用eventmachine来创建很多http查询。 Http服务器可以执行这些连接。 但是对100个查询定期调用5-7 errback。 为什么会这样?很多查询到eventmachine

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 
} 
+0

什么样的错误?这可能与连接的另一端有关,例如,如果其他服务器只能处理50个并发连接,它可能会在超出连接时开始拒绝连接 – 2011-12-26 18:18:07

+0

仅供参考,您可能需要查看[Typhoeus及其关联Hydra](https://github.com/dbalatero/typhoeus),如果你想异步执行多个HTTP请求。 – 2011-12-26 20:48:38

回答

0

您的本地主机可以处理多少个查询?你在127.0.0.1发送100个并发请求,它能处理100个并发请求吗?否则,这些请求将排队并可能在EM :: HttpRequest中超时。

EM :: HttpRequest默认连接超时为5秒,默认不活动超时为10秒。

尝试在本地主机上运行ab,并查看它可以处理多少次请求。