Class: Async::WorkerPool::Worker
- Inherits:
-
Object
- Object
- Async::WorkerPool::Worker
- Defined in:
- lib/async/worker_pool.rb
Overview
A background worker thread.
Instance Method Summary collapse
-
#call(work) ⇒ Object
Call the work and notify the scheduler when it is done.
-
#close ⇒ Object
Close the worker thread.
-
#initialize ⇒ Worker
constructor
Create a new worker.
-
#run ⇒ Object
Execute work until the queue is closed.
Constructor Details
#initialize ⇒ Worker
Create a new worker.
108 109 110 111 |
# File 'lib/async/worker_pool.rb', line 108 def initialize @work = ::Thread::Queue.new @thread = ::Thread.new(&method(:run)) end |
Instance Method Details
#call(work) ⇒ Object
Call the work and notify the scheduler when it is done.
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/async/worker_pool.rb', line 129 def call(work) promise = Promise.new(work) @work.push(promise) begin return promise.wait ensure promise.cancel end end |
#close ⇒ Object
Close the worker thread.
121 122 123 124 125 126 |
# File 'lib/async/worker_pool.rb', line 121 def close if thread = @thread @thread = nil thread.kill end end |
#run ⇒ Object
Execute work until the queue is closed.
114 115 116 117 118 |
# File 'lib/async/worker_pool.rb', line 114 def run while work = @work.pop work.call end end |