我想知道如何使用 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
还有一种宝石叫Parallel https://github.com/grosser/parallel与 Peach 类似,但正在积极更新。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)