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

new() click to toggle source
# File lib/chef/util/threaded_job_queue.rb, line 36
def initialize
  @queue = Queue.new
  @lock = Mutex.new
end

Public Instance Methods

<<(job) click to toggle source
# File lib/chef/util/threaded_job_queue.rb, line 41
def <<(job)
  @queue << job
end
process(concurrency = 10) click to toggle 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