Class: ParallelEach

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/minitest/parallel_each.rb

Overview

Provides a parallel #each that lets you enumerate using N threads. Use environment variable N to customize. Defaults to 2. Enumerable, so all the goodies come along (tho not all are wrapped yet to return another ParallelEach instance).

Constant Summary collapse

N =

How many Threads to use for this parallel #each.

(ENV['N'] || 2).to_i

Instance Method Summary collapse

Constructor Details

#initialize(list) ⇒ ParallelEach

Create a new ParallelEach instance over list.


26
27
28
29
30
31
# File 'lib/minitest/parallel_each.rb', line 26

def initialize list
  @queue = Queue.new # *sigh*... the Queue api sucks sooo much...

  list.each { |i| @queue << i }
  N.times { @queue << nil }
end

Instance Method Details

#countObject Also known as: size


59
60
61
# File 'lib/minitest/parallel_each.rb', line 59

def count
  [@queue.size - N, 0].max
end

#eachObject

Starts N threads that yield each element to your block. Joins the threads at the end.


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/minitest/parallel_each.rb', line 47

def each
  threads = N.times.map {
    Thread.new do
      Thread.current.abort_on_exception = true
      while job = @queue.pop
        yield job
      end
    end
  }
  threads.map(&:join)
end

#grep(pattern) ⇒ Object

:nodoc:


33
34
35
# File 'lib/minitest/parallel_each.rb', line 33

def grep pattern # :nodoc:
  self.class.new super
end

#select(&block) ⇒ Object Also known as: find_all

:nodoc:


37
38
39
# File 'lib/minitest/parallel_each.rb', line 37

def select(&block) # :nodoc:
  self.class.new super
end