I am wondering how i can go about opening multiple concurrent connections using open-uri? i THINK I need to use threading or fibers some how but i\'m not sure.
Exampl
I hope this gives you an idea:
def do_something(url, secs)
sleep secs #just to see a difference
puts "Done with: #{url}"
end
threads = []
urls_ary = ['url1', 'url2', 'url3']
urls_ary.each_with_index do |url, i|
threads << Thread.new{ do_something(url, i+1) }
puts "Out of loop #{i+1}"
end
threads.each{|t| t.join}
Perhaps creating a method for Array
like:
class Array
def thread_each(&block)
inject([]){|threads,e| threads << Thread.new{yield(e)}}.each{|t| t.join}
end
end
[1, 2, 3].thread_each do |i|
sleep 4-i #so first one ends later
puts "Done with #{i}"
end