Skip to content

Instantly share code, notes, and snippets.

@ebisawa
Created January 25, 2011 05:46
Show Gist options
  • Save ebisawa/794565 to your computer and use it in GitHub Desktop.
Save ebisawa/794565 to your computer and use it in GitHub Desktop.
parallel
THREADS = 8
THREAD_TIMEOUT = 180
def xyield(q, &block)
t = Thread.new(Thread.current) do |u|
sleep THREAD_TIMEOUT
puts "-- timeout --"
u.raise
end
begin
yield(q)
rescue
end
t.kill
end
def parallel(queue, num_threads, &block)
threads = []
num_threads.times do
t = Thread.new do
begin
while q = queue.pop(true)
xyield(q, &block)
end
rescue
end
end
threads.push(t)
end
threads.each {|t| t.join }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment