如何使用Ruby(和open-uri)并行处理数组中的项目 [英] How to Process Items in an Array in Parallel using Ruby (and open-uri)
本文介绍了如何使用Ruby(和open-uri)并行处理数组中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何使用open-uri打开多个并发连接?我认为我需要以某种方式使用线程或光纤,但我不确定.
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.
示例代码:
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
推荐答案
还有一个名为平行的宝石.与Peach类似,但正在积极更新.
There's also a gem called Parallel which is similar to Peach, but is actively updated.
这篇关于如何使用Ruby(和open-uri)并行处理数组中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文