Class: Async::WorkerPool::Worker

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

Overview

A background worker thread.

Instance Method Summary collapse

Constructor Details

#initializeWorker

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

#closeObject

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

#runObject

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