Class: BooticCli::WorkerPool

Inherits:
Object
  • Object
show all
Defined in:
lib/bootic_cli/worker_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(how_many) ⇒ WorkerPool

Returns a new instance of WorkerPool.



4
5
6
7
# File 'lib/bootic_cli/worker_pool.rb', line 4

def initialize(how_many)
  @how_many = how_many
  @queue = Queue.new
end

Instance Method Details

#schedule(&block) ⇒ Object



9
10
11
# File 'lib/bootic_cli/worker_pool.rb', line 9

def schedule(&block)
  @queue.push block
end

#startObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bootic_cli/worker_pool.rb', line 13

def start
  threads = @how_many.times.map do |i|
    Thread.new do
      begin
        while job = @queue.pop(true)
          job.call
        end
      rescue ThreadError
      end
    end
  end
  threads.map(&:join)
end