Skip to content

Instantly share code, notes, and snippets.

@ebisawa
Created September 6, 2011 04:44
Show Gist options
  • Save ebisawa/1196608 to your computer and use it in GitHub Desktop.
Save ebisawa/1196608 to your computer and use it in GitHub Desktop.
parallel_each
module Enumerable
DEFAULT_PARALLEL = 8
def parallel_each(num = DEFAULT_PARALLEL)
threads = []
queue = Queue.new
num.times do
threads << Thread.new do
while (a = queue.pop) != nil
yield(a)
end
end
end
self.each {|a| queue << a }
self.size.times { queue << nil }
threads.each {|t| t.join }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment