Skip to content

bpo-29595: Add queue size for pool. #23864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Lib/concurrent/futures/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,25 @@ class BrokenThreadPool(_base.BrokenExecutor):
Raised when a worker thread in a ThreadPoolExecutor failed initializing.
"""

class RejectedExecutionException(Exception):
"""
Raised when the work queue is full.
"""


class ThreadPoolExecutor(_base.Executor):

# Used to assign unique thread names when thread_name_prefix is not supplied.
_counter = itertools.count().__next__

def __init__(self, max_workers=None, thread_name_prefix='',
def __init__(self, max_workers=None, thread_name_prefix='',queue_size=0,
initializer=None, initargs=()):
"""Initializes a new ThreadPoolExecutor instance.

Args:
max_workers: The maximum number of threads that can be used to
execute the given calls.
thread_name_prefix: An optional name prefix to give our threads.
queue_size: The size limit for work queue.
initializer: A callable used to initialize worker threads.
initargs: A tuple of arguments to pass to the initializer.
"""
Expand All @@ -141,7 +146,7 @@ def __init__(self, max_workers=None, thread_name_prefix='',
raise TypeError("initializer must be a callable")

self._max_workers = max_workers
self._work_queue = queue.SimpleQueue()
self._work_queue = queue.Queue(queue_size)
self._idle_semaphore = threading.Semaphore(0)
self._threads = set()
self._broken = False
Expand All @@ -166,7 +171,10 @@ def submit(self, fn, /, *args, **kwargs):
f = _base.Future()
w = _WorkItem(f, fn, args, kwargs)

self._work_queue.put(w)
try:
self._work_queue.put(w)
except queue.Full:
raise RejectedExecutionException()
self._adjust_thread_count()
return f
submit.__doc__ = _base.Executor.submit.__doc__
Expand Down