Module: ParallelRunner

Defined in:
lib/parallel_runner.rb

Class Method Summary collapse

Class Method Details

.each(object, concurency = 10, qsize = nil, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/parallel_runner.rb', line 5

def self.each(object, concurency = 10, qsize = nil, &block)
  q = qsize ? SizedQueue.new(qsize) : Queue.new
  producer = Thread.start(q, concurency) do |pq, pc|
    if object.instance_of? Array
      object.each_with_index {|value, index| pq.enq([[value, index], true])}
    elsif object.instance_of? Hash
      object.each {|key, value| pq.enq([[key, value], true])}
    end
    pc.times{pq.enq([nil, false])}
  end
  workers = []
  concurency.times do
    workers << Thread.start(q) do |wq|
      elem, flg = wq.deq
      while flg
        block.call(elem[0], elem[1])
        elem, flg = wq.deq
      end
    end
  end
  producer.join
  workers.each{|w| w.join}
end