Module: Enumerable

Defined in:
lib/henshin/ext.rb

Instance Method Summary collapse

Instance Method Details

#each_parallel(n = 10) ⇒ Object

Like an each loop but runs each task in parallel Which will probably be very useful for writing and rendering from t-a-w.blogspot.com/2010/05/very-simple-parallelization-with-ruby.html



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/henshin/ext.rb', line 8

def each_parallel( n=10 )
  todo = Queue.new
  ts = (1..n).map do
    Thread.new do 
      while x = todo.deq
        Exception.ignoring_exceptions { yield(x[0]) }
      end
    end
  end
  each {|x| todo << [x] }
  n.times { todo << nil }
  ts.each {|t| t.join }
end