Class: Threadz::ThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/threadz/thread_pool.rb

Overview

The ThreadPool class contains all the threads available to whatever context has access to it.

Constant Summary collapse

KILL_THRESHOLD =

Default setting for kill threshold: 10

10
THREADS_BUSY_SCORE =

Setting for how much to decrement current kill score by for each queued job: 1

1
THREADS_IDLE_SCORE =

Setting for how much to increment current kill score by for each idle thread: 1

1

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ThreadPool

Creates a new thread pool into which you can queue jobs. There are a number of options:

:initial_size

The number of threads you start out with initially. Also, the minimum number of threads. By default, this is 10.

:maximum_size

The highest number of threads that can be allocated. By default, this is the minimum size x 5.

:kill_threshold

Constant that determines when new threads are needed or when threads can be killed off. If the internally tracked kill score falls to positive kill_threshold, then a thread is killed off and the kill score is reset. If the kill score rises to negative kill_threshold, then a new thread is created and the kill score is reset. Every 0.1 seconds, the state of all threads in the pool is checked. If there is more than one idle thread (and we’re above minimum size), the kill score is incremented by THREADS_IDLE_SCORE for each idle thread. If there are no idle threads (and we’re below maximum size) the kill score is decremented by THREADS_KILL_SCORE for each queued job. If the thread pool is being perfectly utilized (no queued work or idle workers), the kill score will decay and lose 10% of its value. In the default case of kill_threshold=10, if the thread pool is overworked for 10 consecutive checks (that is, 1 second), a new thread will be created and the counter reset. Similarly, if the thread pool is underutilized for 10 consecutive checks, an idle thread will be culled. If you want the thread pool to scale more quickly with demand, try lowering the kill_threshold value.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/threadz/thread_pool.rb', line 33

def initialize(opts={})
  @min_size = opts[:initial_size] || 10 # documented
  @max_size = opts[:maximum_size] || @min_size * 5 # documented

  # This is our main queue for jobs
  @queue = Queue.new
  @worker_threads_count = AtomicInteger.new(0)
  @min_size.times { spawn_thread }
  @killscore = 0
  @killthreshold = opts[:kill_threshold] || KILL_THRESHOLD # documented

  spawn_watch_thread
end

Instance Method Details

#new_batch(opts = {}) ⇒ Object

Return a new batch that’s attached into this thread pool. See Threadz::ThreadPool::Batch for documention on opts.



62
63
64
# File 'lib/threadz/thread_pool.rb', line 62

def new_batch(opts={})
  return Batch.new(self, opts)
end

#process(callback = nil, &block) ⇒ Object

Push a process onto the job queue for the thread pool to pick up. Note that using this method, you can’t keep track of when the job finishes. If you care about when it finishes, use batches.



54
55
56
57
58
# File 'lib/threadz/thread_pool.rb', line 54

def process(callback = nil, &block)
  callback ||= block
  @queue << callback
  nil
end

#thread_countObject



47
48
49
# File 'lib/threadz/thread_pool.rb', line 47

def thread_count
  @worker_threads_count.value
end