我想知道如何使用 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