Class: RelatonBib::WorkersPool

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

Overview

Workers poll.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_workers = 2) ⇒ WorkersPool

Returns a new instance of WorkersPool.



8
9
10
11
12
13
# File 'lib/relaton_bib/workers_pool.rb', line 8

def initialize(num_workers = 2)
  @num_workers = num_workers < 2 ? 2 : num_workers
  @queue = SizedQueue.new(num_workers * 2)
  @result = []
  @nb_hits = 0
end

Instance Attribute Details

#nb_hitsObject

Returns the value of attribute nb_hits.



6
7
8
# File 'lib/relaton_bib/workers_pool.rb', line 6

def nb_hits
  @nb_hits
end

Instance Method Details

#<<(item) ⇒ Object



30
31
32
33
# File 'lib/relaton_bib/workers_pool.rb', line 30

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

#endObject



35
36
37
# File 'lib/relaton_bib/workers_pool.rb', line 35

def end
  @num_workers.times { @queue << :END }
end

#resultObject



25
26
27
28
# File 'lib/relaton_bib/workers_pool.rb', line 25

def result
  @threads.each(&:join)
  @result
end

#sizeObject



39
40
41
# File 'lib/relaton_bib/workers_pool.rb', line 39

def size
  @result.size
end

#worker(&block) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/relaton_bib/workers_pool.rb', line 15

def worker(&block)
  @threads = Array.new @num_workers do
    Thread.new do
      until (item = @queue.pop) == :END
        @result << yield(item) if block
      end
    end
  end
end