Class: Metanorma::WorkersPool
- Inherits:
-
Object
- Object
- Metanorma::WorkersPool
- Defined in:
- lib/metanorma/worker_pool.rb
Instance Method Summary collapse
-
#initialize(workers) ⇒ WorkersPool
constructor
A new instance of WorkersPool.
- #schedule(*args, &block) ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize(workers) ⇒ WorkersPool
Returns a new instance of WorkersPool.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/metanorma/worker_pool.rb', line 3 def initialize(workers) @workers = workers @queue = SizedQueue.new(@workers) @threads = Array.new(@workers) do Thread.new do catch(:exit) do loop do job, args = @queue.pop job.call *args end end end end end |
Instance Method Details
#schedule(*args, &block) ⇒ Object
18 19 20 |
# File 'lib/metanorma/worker_pool.rb', line 18 def schedule(*args, &block) @queue << [block, args] end |
#shutdown ⇒ Object
22 23 24 25 26 27 |
# File 'lib/metanorma/worker_pool.rb', line 22 def shutdown @workers.times do schedule { throw :exit } end @threads.map(&:join) end |