Class: Relaton::WorkersPool

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton/workers_pool.rb

Overview

Workers poll.

Instance Method Summary collapse

Constructor Details

#initialize(workers = 2, &_block) ⇒ WorkersPool

Returns a new instance of WorkersPool.


6
7
8
9
10
11
12
13
14
15
16
# File 'lib/relaton/workers_pool.rb', line 6

def initialize(workers = 2, &_block)
  num_workers = workers < 2 ? 2 : workers
  @queue = SizedQueue.new(num_workers * 2)
  @threads = Array.new num_workers do
    Thread.new do
      while item = @queue.pop
        yield(item)
      end
    end
  end
end

Instance Method Details

#<<(item) ⇒ Object


18
19
20
21
# File 'lib/relaton/workers_pool.rb', line 18

def <<(item)
  @queue << item
  self
end