4

I am using Ruby curb to call multiple urls at once, e.g.

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

The problem I have is I want to break the subsequent async calls when any url timeout occurred, is it possible?

Howard
  • 19,215
  • 35
  • 112
  • 184

2 Answers2

0

As far as I know the current API doesn't expose the Curl::Multi instance, since otherwise you could do:


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

The easiest way might be to patch the Curl::Multi.http to return the m variable.

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

Roman
  • 13,100
  • 2
  • 47
  • 63
0

I think this will do exactly what you ask for:

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 }
drKreso
  • 1,030
  • 10
  • 16