Class: Async::WorkerPool
- Inherits:
-
Object
- Object
- Async::WorkerPool
- Defined in:
- lib/async/worker_pool.rb
Overview
A simple work pool that offloads work to a background thread.
Defined Under Namespace
Modules: BlockingOperationWait Classes: Promise, Worker
Instance Method Summary collapse
-
#call(work) ⇒ Object
Offload work to a thread.
-
#close ⇒ Object
Close the work pool.
-
#initialize(size: Etc.nprocessors) ⇒ WorkerPool
constructor
Create a new work pool.
Constructor Details
#initialize(size: Etc.nprocessors) ⇒ WorkerPool
Create a new work pool.
132 133 134 135 136 137 138 |
# File 'lib/async/worker_pool.rb', line 132 def initialize(size: Etc.nprocessors) @ready = ::Thread::Queue.new size.times do @ready.push(Worker.new) end end |
Instance Method Details
#call(work) ⇒ Object
Offload work to a thread.
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/async/worker_pool.rb', line 155 def call(work) if ready = @ready worker = ready.pop begin worker.call(work) ensure ready.push(worker) end else raise RuntimeError, "No worker available!" end end |
#close ⇒ Object
Close the work pool. Kills all outstanding work.
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/async/worker_pool.rb', line 141 def close if ready = @ready @ready = nil ready.close while worker = ready.pop worker.close end end end |