class ChefFS::Parallelizer

Public Class Methods

new(threads) click to toggle source
# File lib/chef_fs/parallelizer.rb, line 18
def initialize(threads)
  @tasks_mutex = Mutex.new
  @tasks = []
  @threads = []
  1.upto(threads) do
    @threads << Thread.new { worker_loop }
  end
end
parallelize(enumerator, options = {}, &block) click to toggle source
# File lib/chef_fs/parallelizer.rb, line 13
def self.parallelize(enumerator, options = {}, &block)
  @@parallelizer ||= Parallelizer.new(@@threads)
  @@parallelizer.parallelize(enumerator, options, &block)
end
threads=(value) click to toggle source
# File lib/chef_fs/parallelizer.rb, line 6
def self.threads=(value)
  if @@threads != value
    @@threads = value
    @@parallelizer = nil
  end
end

Public Instance Methods

parallelize(enumerator, options = {}, &block) click to toggle source
# File lib/chef_fs/parallelizer.rb, line 27
def parallelize(enumerator, options = {}, &block)
  task = ParallelizedResults.new(enumerator, options, &block)
  @tasks_mutex.synchronize do
    @tasks << task
  end
  task
end

Private Instance Methods

worker_loop() click to toggle source
# File lib/chef_fs/parallelizer.rb, line 106
def worker_loop
  while true
    begin
      task = @tasks[0]
      if task
        if !task.process_input
          @tasks_mutex.synchronize do
            @tasks.delete(task)
          end
        end
      else
        # Ruby 1.8 threading sucks.  Wait a bit to see if another task comes in.
        sleep(0.05)
      end
    rescue
      puts "ERROR #{$!}"
      puts $!.backtrace
    end
  end
end