From 273043c42cb08cb42ff9dbcc462824cf6ff9e3ae Mon Sep 17 00:00:00 2001 From: tianc777 <45583542+tianc777@users.noreply.github.com> Date: Sun, 20 Dec 2020 09:52:19 +0800 Subject: [PATCH 1/2] Update thread.py --- Lib/concurrent/futures/thread.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/concurrent/futures/thread.py b/Lib/concurrent/futures/thread.py index b7a2cac7f57015..0a470dbdac1938 100644 --- a/Lib/concurrent/futures/thread.py +++ b/Lib/concurrent/futures/thread.py @@ -114,10 +114,9 @@ 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. @@ -141,7 +140,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 From 42370c8dc2ff76bd744698debb72abbf5e3833ff Mon Sep 17 00:00:00 2001 From: tianc777 <45583542+tianc777@users.noreply.github.com> Date: Sun, 20 Dec 2020 10:25:48 +0800 Subject: [PATCH 2/2] Update thread.py --- Lib/concurrent/futures/thread.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/concurrent/futures/thread.py b/Lib/concurrent/futures/thread.py index 0a470dbdac1938..27bba27ad5cee8 100644 --- a/Lib/concurrent/futures/thread.py +++ b/Lib/concurrent/futures/thread.py @@ -108,6 +108,11 @@ 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): @@ -121,6 +126,7 @@ def __init__(self, max_workers=None, thread_name_prefix='',queue_size=0, 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. """ @@ -165,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__