Tag: 开口uri

如何使用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