Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wtn/370634 to your computer and use it in GitHub Desktop.
Save wtn/370634 to your computer and use it in GitHub Desktop.
@@hydra ||= Typhoeus::Hydra.new( max_concurrency: 100 )
def batch_download(pages)
fibers = pages.map(&:uncached_download_fiber).compact
requests = fibers.map(&:resume)
requests.each {|req| @@hydra.queue req }
@@hydra.run
requests.map.with_index {|req,i| fibers[i].resume(req.response) }
end
def uncached_download_fiber
cached? ? nil : process_markup
end
def process_markup
request = Typhoeus::Request.new(url)
Fiber.new do
response = Fiber.yield(request)
response.code == 200 and File.open(file_path, 'w') {|f| f.puts(remote_markup) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment