Created
October 24, 2012 20:13
-
-
Save jlstr/3948548 to your computer and use it in GitHub Desktop.
Parallel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def crawl_in_parallel(urls=[], type, subcategory) | |
hydra = Typhoeus::Hydra.new(:max_concurrency => 5) | |
urls.each do |url| | |
request = Typhoeus::Request.new(url, | |
timeout: 7000, | |
follow_location: true, | |
) | |
request.on_complete do |response| | |
if response.success? | |
begin | |
ShopbopProductWrapper.new(url, type, subcategory, response.body) | |
rescue Exception => e | |
print "F (#{e.message})" | |
end | |
elsif response.timed_out? | |
puts "TIMEOUT for: #{url}" | |
elsif response.code == 0 | |
puts "FAILED HTTP RESPONSE FOR: #{url}" | |
else | |
puts "HTTP request failed: #{response.code.to_s}" | |
end | |
end | |
hydra.queue request | |
end | |
hydra.run | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment