如何使用 Ruby(和 open-uri)并行处理数组中的项目
我想知道如何使用 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
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.
Example code:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
还有一个名为 Parallel 的 gem,它与 Peach 类似,但正在积极更新。
There's also a gem called Parallel which is similar to Peach, but is actively updated.
我希望这能给您一个想法:
也许为
Array
创建一个方法,如下所示:I hope this gives you an idea:
Perhaps creating a method for
Array
like:用法:
Usage:
使用线程的简单方法:
A simple method using threads:
有一种名为
peach
的 gem (https://rubygems.org/gems/peach) 它可以让你这样做:There is a gem called
peach
(https://rubygems.org/gems/peach) which lets you do this: