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

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.



19
20
21
22
23
24
# File 'lib/minitest/parallel_each.rb', line 19

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



52
53
54
# File 'lib/minitest/parallel_each.rb', line 52

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.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/minitest/parallel_each.rb', line 40

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:



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

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

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

:nodoc:



30
31
32
# File 'lib/minitest/parallel_each.rb', line 30

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