Skip to content

Instantly share code, notes, and snippets.

@kennyp
Created April 23, 2013 15:21
Show Gist options
  • Save kennyp/5444483 to your computer and use it in GitHub Desktop.
Save kennyp/5444483 to your computer and use it in GitHub Desktop.
Run concurrently with a progress bar.
def run_concurrently(title, jobs, &block)
mutex = Mutex.new
work = Queue.new
jobs.each {|j| work << j}
pbar = ProgressBar.create(title: title, total: jobs.length, format: '%a |%w>%i| %e %t')
threads = 4.times.map do
Thread.new do
loop do
block.call(work.pop(:nonblocking))
mutex.synchronize { pbar.increment }
end
end
end
sleep 5 while threads.any?(&:alive?)
pbar.finish
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment