A simple threaded job queue
Create a queue:
queue = ThreadedJobQueue.new
Add jobs:
queue << lambda { |lock| foo.the_bar }
A job is a callable that optionally takes a Mutex instance as its only parameter.
Then start processing jobs with n threads:
queue.process(n)
# File lib/chef/util/threaded_job_queue.rb, line 43 def <<(job) @queue << job end
# File lib/chef/util/threaded_job_queue.rb, line 47 def process(concurrency = 10) workers = (1..concurrency).map do Thread.new do loop do fn = @queue.pop fn.arity == 1 ? fn.call(@lock) : fn.call end end end workers.each { |worker| self << Thread.method(:exit) } workers.each { |worker| worker.join } end
Generated with the Darkfish Rdoc Generator 2.