如何在使用ruby / curb的url超时时退出异步调用

时间:2014-04-01 16:26:34

标签: ruby curb

我正在使用Ruby curb一次调用多个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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 }