Class: Async::WorkerPool::Worker

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

Overview

A handle to the work being done.

Instance Method Summary collapse

Constructor Details

#initializeWorker

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

#closeObject



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

#runObject



102
103
104
105
106
# File 'lib/async/worker_pool.rb', line 102

def run
	while work = @work.pop
		work.call
	end
end