Class: Async::WorkerPool::Worker
- Inherits:
-
Object
- Object
- Async::WorkerPool::Worker
- Defined in:
- lib/async/worker_pool.rb
Overview
A handle to the work being done.
Instance Method Summary collapse
-
#call(work) ⇒ Object
Call the work and notify the scheduler when it is done.
- #close ⇒ Object
-
#initialize ⇒ Worker
constructor
A new instance of Worker.
- #run ⇒ Object
Constructor Details
#initialize ⇒ Worker
Returns a new instance of Worker.
97 98 99 100 |
# File 'lib/async/worker_pool.rb', line 97 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.
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/async/worker_pool.rb', line 116 def call(work) promise = Promise.new(work) @work.push(promise) begin return promise.wait ensure promise.cancel end end |
#close ⇒ Object
108 109 110 111 112 113 |
# File 'lib/async/worker_pool.rb', line 108 def close if thread = @thread @thread = nil thread.kill end end |
#run ⇒ Object
102 103 104 105 106 |
# File 'lib/async/worker_pool.rb', line 102 def run while work = @work.pop work.call end end |