Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f40a26cd65
Fetching contributors…

Cannot retrieve contributors at this time

51 lines (40 sloc) 1.121 kb
##
# 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).
if defined?(RUBY_ENGINE) && RUBY_ENGINE == "maglev" then # HACK - need to debug
class ParallelEach < Array; end
else
class ParallelEach
require 'thread'
include Enumerable
##
# How many Threads to use for this parallel #each.
N = (ENV['N'] || 2).to_i
##
# Create a new ParallelEach instance over +list+.
def initialize list
@queue = Queue.new # *sigh*... the Queue api sucks sooo much...
list.each { |i| @queue << i }
N.times { @queue << nil }
end
def grep pattern # :nodoc:
self.class.new super
end
##
# Starts N threads that yield each element to your block. Joins the
# threads at the end.
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
end
end
Jump to Line
Something went wrong with that request. Please try again.