Sha256: abab75713367bf6ae4aa93b79738494c528670071f6d58ca4238d8017bde3b75

Contents?: true

Size: 585 Bytes

Versions: 10

Compression:

Stored size: 585 Bytes

Contents

module Kraps
  class Parallelizer
    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
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
kraps-0.10.0 lib/kraps/parallelizer.rb
kraps-0.9.0 lib/kraps/parallelizer.rb
kraps-0.8.0 lib/kraps/parallelizer.rb
kraps-0.7.0 lib/kraps/parallelizer.rb
kraps-0.6.0 lib/kraps/parallelizer.rb
kraps-0.5.0 lib/kraps/parallelizer.rb
kraps-0.4.0 lib/kraps/parallelizer.rb
kraps-0.3.0 lib/kraps/parallelizer.rb
kraps-0.2.0 lib/kraps/parallelizer.rb
kraps-0.1.0 lib/kraps/parallelizer.rb