How to Process Items in an Array in Parallel using Ruby (and open-uri)

前端 未结 5 1647
灰色年华
灰色年华 2021-02-20 12:41

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.

Exampl

5条回答
  •  情深已故
    2021-02-20 13:19

    module MultithreadedEach
      def multithreaded_each
        each_with_object([]) do |item, threads|
          threads << Thread.new { yield item }
        end.each { |thread| thread.join }
        self
      end
    end
    

    Usage:

    arr = [1,2,3]
    
    arr.extend(MultithreadedEach)
    
    arr.multithreaded_each do |n|
      puts n # Each block runs in it's own thread
    end
    

提交回复
热议问题