是否可以使用带有Ruby 1.8的EventMachine启动多个并行http请求

是否可以使用带有Ruby 1.8的EventMachine启动多个并行http请求,ruby,multithreading,eventmachine,Ruby,Multithreading,Eventmachine,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 看看: 如果您可以看看EventMachine之外的东西,它是Hy

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
看看:


如果您可以看看EventMachine之外的东西,它是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
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