Class: Yarn::WorkerPool

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/yarn/worker_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #log, #output, #timestamp

Constructor Details

#initialize(size = 1024) ⇒ WorkerPool

Returns a new instance of WorkerPool.



14
15
16
17
18
# File 'lib/yarn/worker_pool.rb', line 14

def initialize(size=1024)
  @size = size
  @workers = create_pool
  @jobs = Queue.new
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



12
13
14
# File 'lib/yarn/worker_pool.rb', line 12

def size
  @size
end

#workersObject (readonly)

Returns the value of attribute workers.



12
13
14
# File 'lib/yarn/worker_pool.rb', line 12

def workers
  @workers
end

Instance Method Details

#create_poolObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/yarn/worker_pool.rb', line 20

def create_pool
  return Array.new(@size) do
    Thread.new do
      loop do
        job = @jobs.pop
        job.call
      end
    end
  end
end

#schedule(&block) ⇒ Object



31
32
33
# File 'lib/yarn/worker_pool.rb', line 31

def schedule(&block)
  @jobs << block
end