Class: Metanorma::Util::WorkersPool

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

Instance Method Summary collapse

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_threadObject



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

#shutdownObject



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