Class: Async::Worker::Pool
- Inherits:
-
Object
- Object
- Async::Worker::Pool
- Defined in:
- lib/async/worker/pool.rb
Instance Method Summary collapse
- #async(&block) ⇒ Object
- #close ⇒ Object
-
#initialize(workers = 4) ⇒ Pool
constructor
A new instance of Pool.
Constructor Details
#initialize(workers = 4) ⇒ Pool
Returns a new instance of Pool.
29 30 31 32 33 34 35 |
# File 'lib/async/worker/pool.rb', line 29 def initialize(workers = 4) @threads = [] @queue = Thread::Queue.new workers.times {create_worker} end |
Instance Method Details
#async(&block) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/async/worker/pool.rb', line 42 def async(&block) notification = Notification.new result = nil job = lambda do begin result = block.call rescue Exception result = $! ensure notification.signal end end @queue << job notification.wait notification.close return result end |
#close ⇒ Object
37 38 39 40 |
# File 'lib/async/worker/pool.rb', line 37 def close @queue.close @threads.each(&:join) end |