Class: Kraps::Parallelizer
- Inherits:
-
Object
- Object
- Kraps::Parallelizer
- Defined in:
- lib/kraps/parallelizer.rb
Class Method Summary collapse
Class Method Details
.each(enum, num_threads) ⇒ Object
3 4 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/kraps/parallelizer.rb', line 3 def self.each(enum, num_threads) queue = Queue.new enum.each { |element| queue.push element } stopped = false threads = Array.new(num_threads) do Thread.new do yield queue.pop(true) until stopped || queue.empty? rescue ThreadError # Queue empty rescue StandardError => e stopped = true e end end threads.each(&:join).each do |thread| raise thread.value if thread.value.is_a?(Exception) end enum end |