4

我正在使用 Ruby 路边一次调用多个 url,例如

require 'rubygems'
require 'curb'

easy_options = {:follow_location => true}
multi_options = {:pipeline => true}

Curl::Multi.get(['http://www.example.com','http://www.trello.com','http://www.facebook.com','http://www.yahoo.com','http://www.msn.com'], easy_options, multi_options) do|easy|
  # do something interesting with the easy response
  puts easy.last_effective_url
end

我遇到的问题是我想在发生任何 url 超时时中断后续的异步调用,这可能吗?

4

2 回答 2

0

据我所知,当前的 API 没有公开 Curl::Multi 实例,否则你可以这样做:


stop_everything = proc { multi.cancel! }
multi = Curl::Multi.get(array_of_urls, on_failure: stop_everything)

最简单的方法可能是修补 Curl::Multi.http 以返回 m 变量。

https://github.com/taf2/curb/blob/master/lib/curl/multi.rb#L85

于 2014-04-07T18:14:45.127 回答
0

我认为这将完全满足您的要求:

require 'rubygems'
require 'curb'

responses = {}
requests = ['http://www.example.com','http://www.trello.com','http://www.facebook.com','http://www.yahoo.com','http://www.msn.com']
m = Curl::Multi.new
requests.each do |url|
  responses[url] = ""
    c = Curl::Easy.new(url) do|curl|
    curl.follow_location = true
    curl.on_body{|data| responses[url] << data; data.size }
    curl.on_success {|easy| puts easy.last_effective_url }
    curl.on_failure {|easy| puts "ERROR:#{easy.last_effective_url}"; @should_stop = true}
  end
  m.add(c)
end

m.perform { m.cancel! if @should_stop }
于 2014-04-13T18:27:13.743 回答