Class: ChefUtils::DefaultThreadPool

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/chef-utils/parallel_map.rb

Overview

The DefaultThreadPool has a fixed thread size and has no queue of work and the behavior on failure to find a thread is for the caller to run the work. This contract means that the thread pool can be called recursively without deadlocking and while keeping the fixed number of threads (and not exponentially growing the thread pool with the depth of recursion).

Constant Summary collapse

DEFAULT_THREAD_SIZE =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#threadsInteger

Size of the thread pool, must be set before getting the thread pool or calling parallel_map/parallel_each. Does not (but could be modified to) support dynamic resizing. To get fully synchronous behavior set this equal to zero rather than one since the caller will get work if the threads are busy.

Returns:

  • (Integer)

    number of threads



113
114
115
# File 'lib/chef-utils/parallel_map.rb', line 113

def threads
  @threads
end

Instance Method Details

#poolConcurrent::ThreadPoolExecutor

Memoizing accessor for the thread pool

Returns:

  • (Concurrent::ThreadPoolExecutor)

    the thread pool



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/chef-utils/parallel_map.rb', line 118

def pool
  @pool ||= Concurrent::ThreadPoolExecutor.new(
    min_threads: threads || DEFAULT_THREAD_SIZE,
    max_threads: threads || DEFAULT_THREAD_SIZE,
    max_queue: 0,
    # "synchronous" redefines the 0 in max_queue to mean 'no queue' instead of 'infinite queue'
    # it does not mean synchronous execution (no threads) but synchronous offload to the threads.
    synchronous: true,
    # this prevents deadlocks on recursive parallel usage
    fallback_policy: :caller_runs
  )
end