【问题标题】:A lot of queries to the eventmachine对事件机器的很多查询
【发布时间】: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


【解决方案1】:

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

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

尝试在您的本地主机上运行 ab,看看它每秒可以处理多少个请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2012-08-27
    • 2020-05-02
    • 2018-07-24
    相关资源
    最近更新 更多