Skip to content

Instantly share code, notes, and snippets.

@iwan
Forked from a-chernykh/net_http.rb
Last active December 29, 2015 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwan/d417767e850f5ef61dec to your computer and use it in GitHub Desktop.
Save iwan/d417767e850f5ef61dec to your computer and use it in GitHub Desktop.
require 'net/http'
def download_net_http(uris, thread_count)
queue = Queue.new
uris.map { |uri| queue << uri }
threads = thread_count.times.map do
Thread.new do
while !queue.empty? && uri = queue.pop
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme=='https') do |http|
request = Net::HTTP::Get.new(uri)
response = http.request request
write_file filename, response.body
end
end
end
end
threads.each(&:join)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment