gpt4 book ai didi

ruby - 如何使用 Ruby(和 open-uri)并行处理数组中的项目

转载 作者:数据小太阳 更新时间:2023-10-29 06:45:25 25 4
gpt4 key购买 nike

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

最佳答案

还有一个 gem 叫做 Parallel与 Peach 类似,但会主动更新。

关于ruby - 如何使用 Ruby(和 open-uri)并行处理数组中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7561629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com