class Chef::Util::ThreadedJobQueue
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)
Public Class Methods
Source
# File lib/chef/util/threaded_job_queue.rb, line 36 def initialize @queue = Queue.new @lock = Mutex.new end
Public Instance Methods
Source
# File lib/chef/util/threaded_job_queue.rb, line 45 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(&:join) end