2014-04-01 94 views
4

我使用Ruby curb一次調用多個url,如何在使用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 

這個問題我已經是我想要的任何URL超時發生時打破後續異步調用,這可能嗎?

回答

0

據我所知,目前API不公開卷曲::多實例,因爲否則的話,你可以這樣做:


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

最簡單的方法可能是修補捲曲:: Multi.http返回m變量。

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

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 } 
相關問題