Mar*_*tto 10 ruby multithreading open-uri fibers
我想知道如何使用open-uri打开多个并发连接?我认为我需要使用线程或纤维,但我不确定.
示例代码:
def get_doc(url)
begin
Nokogiri::HTML(open(url).read)
rescue Exception => ex
puts "Failed at #{Time.now}"
puts "Error: #{ex}"
end
end
array_of_urls_to_process = [......]
# How can I iterate over items in the array in parallel (instead of one at a time?)
array_of_urls_to_process.each do |url|
x = get_doc(url)
do_something(x)
end
Run Code Online (Sandbox Code Playgroud)
我希望这会给你一个想法:
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}
Run Code Online (Sandbox Code Playgroud)
也许创建一个Array类似的方法:
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
Run Code Online (Sandbox Code Playgroud)