Class: Metanorma::Util::WorkersPool
- Inherits:
-
Object
- Object
- Metanorma::Util::WorkersPool
- Defined in:
- lib/metanorma/util/worker_pool.rb
Instance Method Summary collapse
- #init_thread ⇒ Object
- #init_vars(workers) ⇒ Object
-
#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.
4 5 6 7 8 9 |
# File 'lib/metanorma/util/worker_pool.rb', line 4 def initialize(workers) init_vars(workers) @threads = Array.new(@workers) do init_thread end end |
Instance Method Details
#init_thread ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/metanorma/util/worker_pool.rb', line 16 def init_thread Thread.new do catch(:exit) do loop do job, args = @queue.pop job.call *args end end end end |
#init_vars(workers) ⇒ Object
11 12 13 14 |
# File 'lib/metanorma/util/worker_pool.rb', line 11 def init_vars(workers) @workers = workers @queue = SizedQueue.new(@workers) end |
#schedule(*args, &block) ⇒ Object
27 28 29 |
# File 'lib/metanorma/util/worker_pool.rb', line 27 def schedule(*args, &block) @queue << [block, args] end |
#shutdown ⇒ Object
31 32 33 34 35 36 |
# File 'lib/metanorma/util/worker_pool.rb', line 31 def shutdown @workers.times do schedule { throw :exit } end @threads.map(&:join) end |