Skip to content

Instantly share code, notes, and snippets.

@flaviokr
Last active December 1, 2017 02:19
Show Gist options
  • Save flaviokr/b615023bd77c1c50cbb7a588002658f8 to your computer and use it in GitHub Desktop.
Save flaviokr/b615023bd77c1c50cbb7a588002658f8 to your computer and use it in GitHub Desktop.
# If you want to provide a maximum queue size, you may also consider the fallback_policy which defines what will
# happen if work is posted to a pool when the queue of waiting work has reached the maximum size and no new threads
# can be created. Available policies:
# abort: Raise a Concurrent::RejectedExecutionError exception and discard the task. (default policy)
# discard: Silently discard the task and return nil as the task result.
# caller_runs: The work will be executed in the thread of the caller, instead of being given to another thread in the pool.
pool = Concurrent::ThreadPoolExecutor.new(
:min_threads => [2, Concurrent.processor_count].max,
:max_threads => [2, Concurrent.processor_count].max,
:max_queue => [2, Concurrent.processor_count].max * 5,
:fallback_policy => :caller_runs
)
future = Future.execute(:executor => pool) do
#work
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment