Class: Ethreadpool::Threadpool

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

Constant Summary collapse

INIT_WORKERS =
10
MAX_WORKERS =
20
TIMEOUT_SECS =
30

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Threadpool

Returns a new instance of Threadpool.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/threadpool/core.rb', line 10

def initialize(*args)
  extend MonitorMixin
  @init_workers = args.shift || INIT_WORKERS
  @max_workers = args.shift || MAX_WORKERS
  @timeout_secs = args.shift || TIMEOUT_SECS
  if @max_workers < @init_workers
    raise "max_workers cannot smaller than init_workers"
  end

  @workers = (0...@init_workers).map { Threadpool::Worker.new }
  Thread.new { process }
end

Instance Method Details

#load(job) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/threadpool/core.rb', line 23

def load(job)
  if @teminate
    puts "job: #{job.jobid} cannot be loaded, the pool is terminating."
    return
  end

  worker = nil
  loop do
    worker = idle_worker
    worker.nil? ? create_worker : break
    sleep(0.001)
  end

  worker.job = job
end

#shutdownObject



39
40
41
42
43
44
45
46
# File 'lib/threadpool/core.rb', line 39

def shutdown
  @teminate = true

  loop do
    return if busy_workers.count == 0
    sleep(0.001)
  end
end