Class: PWork::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/pwork/worker.rb

Instance Method Summary collapse

Constructor Details

#initialize(threads: 5) ⇒ Worker

Returns a new instance of Worker.



4
5
6
7
# File 'lib/pwork/worker.rb', line 4

def initialize(threads: 5)
  @processes = []
  @thread_count = threads
end

Instance Method Details

#add(&block) ⇒ Object

This method is called to add a process to this worker



10
11
12
# File 'lib/pwork/worker.rb', line 10

def add(&block)
  @processes.push(block)
end

#executeObject

This method is called to execute all processes attached to this worker in parallel on the specified number of threads.



15
16
17
18
19
# File 'lib/pwork/worker.rb', line 15

def execute
  @processes.peach(@thread_count) do |item|
    item.call
  end
end