Class: WorkerPool
- Inherits:
-
Object
- Object
- WorkerPool
- Defined in:
- lib/worker_pool.rb,
lib/worker_pool/version.rb,
lib/worker_pool/worker_error.rb,
lib/worker_pool/countdown_latch.rb
Defined Under Namespace
Classes: CountdownLatch, WorkerError
Constant Summary collapse
- BUFFER_SIZE =
1
- VERSION =
'0.0.1'
Instance Method Summary collapse
-
#initialize(worker_count) ⇒ WorkerPool
constructor
A new instance of WorkerPool.
- #perform(tasks, timeout, &block) ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize(worker_count) ⇒ WorkerPool
Returns a new instance of WorkerPool.
6 7 8 9 |
# File 'lib/worker_pool.rb', line 6 def initialize(worker_count) @queue = SizedQueue.new(BUFFER_SIZE) @workers = (1..worker_count).map { |i| start_worker(i) } end |
Instance Method Details
#perform(tasks, timeout, &block) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/worker_pool.rb', line 11 def perform(tasks, timeout, &block) countdown, accumulator = CountdownLatch.new(tasks.size), Queue.new tasks.each { |args| enqueue(countdown, accumulator, args, block) } countdown.wait(timeout) queue_to_array(accumulator) end |
#shutdown ⇒ Object
18 19 20 |
# File 'lib/worker_pool.rb', line 18 def shutdown @workers.each(&:kill) end |