Class: Relaton::Core::WorkersPool

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

Overview

Workers poll.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_workers = 2) ⇒ WorkersPool



9
10
11
12
13
14
# File 'lib/relaton/core/workers_pool.rb', line 9

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.



7
8
9
# File 'lib/relaton/core/workers_pool.rb', line 7

def nb_hits
  @nb_hits
end

Instance Method Details

#<<(item) ⇒ Object



31
32
33
34
# File 'lib/relaton/core/workers_pool.rb', line 31

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

#endObject



36
37
38
# File 'lib/relaton/core/workers_pool.rb', line 36

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

#resultObject



26
27
28
29
# File 'lib/relaton/core/workers_pool.rb', line 26

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

#sizeObject



40
41
42
# File 'lib/relaton/core/workers_pool.rb', line 40

def size
  @result.size
end

#worker(&block) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/relaton/core/workers_pool.rb', line 16

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