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 }
  @checker_thread = Thread.new { run_checker }
end

Instance Method Details

#busy_workers_countObject



46
47
48
# File 'lib/threadpool/core.rb', line 46

def busy_workers_count
  busy_workers.count
end

#load(job) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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 here is a must, or MRI will get stucks
    sleep(0.001)
  end

  worker.job = job
end

#shutdownObject



41
42
43
44
# File 'lib/threadpool/core.rb', line 41

def shutdown
  @teminate = true
  @checker_thread.join
end