Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 1 | # Copyright 2009 Brian Quinlan. All Rights Reserved. |
| 2 | # Licensed to PSF under a Contributor Agreement. |
| 3 | |
| 4 | """Implements ThreadPoolExecutor.""" |
| 5 | |
| 6 | __author__ = 'Brian Quinlan (brian@sweetapp.com)' |
| 7 | |
| 8 | import atexit |
| 9 | from concurrent.futures import _base |
Gregory P. Smith | a3d91b4 | 2017-06-21 23:41:13 -0700 | [diff] [blame] | 10 | import itertools |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 11 | import queue |
| 12 | import threading |
| 13 | import weakref |
Guido van Rossum | cfd4661 | 2014-09-02 10:39:18 -0700 | [diff] [blame] | 14 | import os |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 15 | |
| 16 | # Workers are created as daemon threads. This is done to allow the interpreter |
| 17 | # to exit when there are still idle threads in a ThreadPoolExecutor's thread |
| 18 | # pool (i.e. shutdown() was not called). However, allowing workers to die with |
| 19 | # the interpreter has two undesirable properties: |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 20 | # - The workers would still be running during interpreter shutdown, |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 21 | # meaning that they would fail in unpredictable ways. |
| 22 | # - The workers could be killed while evaluating a work item, which could |
| 23 | # be bad if the callable being evaluated has external side-effects e.g. |
| 24 | # writing to a file. |
| 25 | # |
| 26 | # To work around this problem, an exit handler is installed which tells the |
| 27 | # workers to exit when their work queues are empty and then waits until the |
| 28 | # threads finish. |
| 29 | |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 30 | _threads_queues = weakref.WeakKeyDictionary() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 31 | _shutdown = False |
Brian Quinlan | 242c26f | 2019-06-28 11:54:52 -0700 | [diff] [blame^] | 32 | # Lock that ensures that new workers are not created while the interpreter is |
| 33 | # shutting down. Must be held while mutating _threads_queues and _shutdown. |
| 34 | _global_shutdown_lock = threading.Lock() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 35 | |
| 36 | def _python_exit(): |
| 37 | global _shutdown |
Brian Quinlan | 242c26f | 2019-06-28 11:54:52 -0700 | [diff] [blame^] | 38 | with _global_shutdown_lock: |
| 39 | _shutdown = True |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 40 | items = list(_threads_queues.items()) |
| 41 | for t, q in items: |
| 42 | q.put(None) |
| 43 | for t, q in items: |
| 44 | t.join() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 45 | |
| 46 | atexit.register(_python_exit) |
| 47 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 48 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 49 | class _WorkItem(object): |
| 50 | def __init__(self, future, fn, args, kwargs): |
| 51 | self.future = future |
| 52 | self.fn = fn |
| 53 | self.args = args |
| 54 | self.kwargs = kwargs |
| 55 | |
| 56 | def run(self): |
| 57 | if not self.future.set_running_or_notify_cancel(): |
| 58 | return |
| 59 | |
| 60 | try: |
| 61 | result = self.fn(*self.args, **self.kwargs) |
Victor Stinner | bc61315 | 2017-08-22 16:50:42 +0200 | [diff] [blame] | 62 | except BaseException as exc: |
| 63 | self.future.set_exception(exc) |
| 64 | # Break a reference cycle with the exception 'exc' |
| 65 | self = None |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 66 | else: |
| 67 | self.future.set_result(result) |
| 68 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 69 | |
| 70 | def _worker(executor_reference, work_queue, initializer, initargs): |
| 71 | if initializer is not None: |
| 72 | try: |
| 73 | initializer(*initargs) |
| 74 | except BaseException: |
| 75 | _base.LOGGER.critical('Exception in initializer:', exc_info=True) |
| 76 | executor = executor_reference() |
| 77 | if executor is not None: |
| 78 | executor._initializer_failed() |
| 79 | return |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 80 | try: |
| 81 | while True: |
Antoine Pitrou | 27be5da | 2011-04-12 17:48:46 +0200 | [diff] [blame] | 82 | work_item = work_queue.get(block=True) |
| 83 | if work_item is not None: |
| 84 | work_item.run() |
Andrew Svetlov | 6b97374 | 2012-11-03 15:36:01 +0200 | [diff] [blame] | 85 | # Delete references to object. See issue16284 |
| 86 | del work_item |
Sean | 904e34d | 2019-05-22 14:29:58 -0700 | [diff] [blame] | 87 | |
| 88 | # attempt to increment idle count |
| 89 | executor = executor_reference() |
| 90 | if executor is not None: |
| 91 | executor._idle_semaphore.release() |
| 92 | del executor |
Antoine Pitrou | 27be5da | 2011-04-12 17:48:46 +0200 | [diff] [blame] | 93 | continue |
Sean | 904e34d | 2019-05-22 14:29:58 -0700 | [diff] [blame] | 94 | |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 95 | executor = executor_reference() |
| 96 | # Exit if: |
| 97 | # - The interpreter is shutting down OR |
| 98 | # - The executor that owns the worker has been collected OR |
| 99 | # - The executor that owns the worker has been shutdown. |
| 100 | if _shutdown or executor is None or executor._shutdown: |
Mark Nemec | c4b695f | 2018-04-10 18:23:14 +0100 | [diff] [blame] | 101 | # Flag the executor as shutting down as early as possible if it |
| 102 | # is not gc-ed yet. |
| 103 | if executor is not None: |
| 104 | executor._shutdown = True |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 105 | # Notice other workers |
| 106 | work_queue.put(None) |
| 107 | return |
| 108 | del executor |
Florent Xicluna | 04842a8 | 2011-11-11 20:05:50 +0100 | [diff] [blame] | 109 | except BaseException: |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 110 | _base.LOGGER.critical('Exception in worker', exc_info=True) |
| 111 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 112 | |
| 113 | class BrokenThreadPool(_base.BrokenExecutor): |
| 114 | """ |
| 115 | Raised when a worker thread in a ThreadPoolExecutor failed initializing. |
| 116 | """ |
| 117 | |
| 118 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 119 | class ThreadPoolExecutor(_base.Executor): |
Gregory P. Smith | a3d91b4 | 2017-06-21 23:41:13 -0700 | [diff] [blame] | 120 | |
| 121 | # Used to assign unique thread names when thread_name_prefix is not supplied. |
| 122 | _counter = itertools.count().__next__ |
| 123 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 124 | def __init__(self, max_workers=None, thread_name_prefix='', |
| 125 | initializer=None, initargs=()): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 126 | """Initializes a new ThreadPoolExecutor instance. |
| 127 | |
| 128 | Args: |
| 129 | max_workers: The maximum number of threads that can be used to |
| 130 | execute the given calls. |
Gregory P. Smith | 50abe87 | 2016-08-07 10:19:20 -0700 | [diff] [blame] | 131 | thread_name_prefix: An optional name prefix to give our threads. |
ubordignon | 552ace7 | 2019-06-15 13:43:10 +0200 | [diff] [blame] | 132 | initializer: A callable used to initialize worker threads. |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 133 | initargs: A tuple of arguments to pass to the initializer. |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 134 | """ |
Guido van Rossum | cfd4661 | 2014-09-02 10:39:18 -0700 | [diff] [blame] | 135 | if max_workers is None: |
Inada Naoki | 9a7e5b1 | 2019-05-28 21:02:52 +0900 | [diff] [blame] | 136 | # ThreadPoolExecutor is often used to: |
| 137 | # * CPU bound task which releases GIL |
| 138 | # * I/O bound task (which releases GIL, of course) |
| 139 | # |
| 140 | # We use cpu_count + 4 for both types of tasks. |
| 141 | # But we limit it to 32 to avoid consuming surprisingly large resource |
| 142 | # on many core machine. |
| 143 | max_workers = min(32, (os.cpu_count() or 1) + 4) |
Brian Quinlan | 20efceb | 2014-05-17 13:51:10 -0700 | [diff] [blame] | 144 | if max_workers <= 0: |
| 145 | raise ValueError("max_workers must be greater than 0") |
| 146 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 147 | if initializer is not None and not callable(initializer): |
| 148 | raise TypeError("initializer must be a callable") |
| 149 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 150 | self._max_workers = max_workers |
Antoine Pitrou | ab74504 | 2018-01-18 10:38:03 +0100 | [diff] [blame] | 151 | self._work_queue = queue.SimpleQueue() |
Sean | 904e34d | 2019-05-22 14:29:58 -0700 | [diff] [blame] | 152 | self._idle_semaphore = threading.Semaphore(0) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 153 | self._threads = set() |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 154 | self._broken = False |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 155 | self._shutdown = False |
| 156 | self._shutdown_lock = threading.Lock() |
Gregory P. Smith | a3d91b4 | 2017-06-21 23:41:13 -0700 | [diff] [blame] | 157 | self._thread_name_prefix = (thread_name_prefix or |
| 158 | ("ThreadPoolExecutor-%d" % self._counter())) |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 159 | self._initializer = initializer |
| 160 | self._initargs = initargs |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 161 | |
Serhiy Storchaka | 142566c | 2019-06-05 18:22:31 +0300 | [diff] [blame] | 162 | def submit(self, fn, /, *args, **kwargs): |
Brian Quinlan | 242c26f | 2019-06-28 11:54:52 -0700 | [diff] [blame^] | 163 | with self._shutdown_lock, _global_shutdown_lock: |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 164 | if self._broken: |
| 165 | raise BrokenThreadPool(self._broken) |
| 166 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 167 | if self._shutdown: |
| 168 | raise RuntimeError('cannot schedule new futures after shutdown') |
Mark Nemec | c4b695f | 2018-04-10 18:23:14 +0100 | [diff] [blame] | 169 | if _shutdown: |
Serhiy Storchaka | 34fd4c2 | 2018-11-05 16:20:25 +0200 | [diff] [blame] | 170 | raise RuntimeError('cannot schedule new futures after ' |
Mark Nemec | c4b695f | 2018-04-10 18:23:14 +0100 | [diff] [blame] | 171 | 'interpreter shutdown') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 172 | |
| 173 | f = _base.Future() |
| 174 | w = _WorkItem(f, fn, args, kwargs) |
| 175 | |
| 176 | self._work_queue.put(w) |
| 177 | self._adjust_thread_count() |
| 178 | return f |
| 179 | submit.__doc__ = _base.Executor.submit.__doc__ |
| 180 | |
| 181 | def _adjust_thread_count(self): |
Sean | 904e34d | 2019-05-22 14:29:58 -0700 | [diff] [blame] | 182 | # if idle threads are available, don't spin new threads |
| 183 | if self._idle_semaphore.acquire(timeout=0): |
| 184 | return |
| 185 | |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 186 | # When the executor gets lost, the weakref callback will wake up |
| 187 | # the worker threads. |
| 188 | def weakref_cb(_, q=self._work_queue): |
| 189 | q.put(None) |
Sean | 904e34d | 2019-05-22 14:29:58 -0700 | [diff] [blame] | 190 | |
Gregory P. Smith | 50abe87 | 2016-08-07 10:19:20 -0700 | [diff] [blame] | 191 | num_threads = len(self._threads) |
| 192 | if num_threads < self._max_workers: |
| 193 | thread_name = '%s_%d' % (self._thread_name_prefix or self, |
| 194 | num_threads) |
| 195 | t = threading.Thread(name=thread_name, target=_worker, |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 196 | args=(weakref.ref(self, weakref_cb), |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 197 | self._work_queue, |
| 198 | self._initializer, |
| 199 | self._initargs)) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 200 | t.daemon = True |
| 201 | t.start() |
| 202 | self._threads.add(t) |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 203 | _threads_queues[t] = self._work_queue |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 204 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 205 | def _initializer_failed(self): |
| 206 | with self._shutdown_lock: |
| 207 | self._broken = ('A thread initializer failed, the thread pool ' |
| 208 | 'is not usable anymore') |
| 209 | # Drain work queue and mark pending futures failed |
| 210 | while True: |
| 211 | try: |
| 212 | work_item = self._work_queue.get_nowait() |
| 213 | except queue.Empty: |
| 214 | break |
| 215 | if work_item is not None: |
| 216 | work_item.future.set_exception(BrokenThreadPool(self._broken)) |
| 217 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 218 | def shutdown(self, wait=True): |
| 219 | with self._shutdown_lock: |
| 220 | self._shutdown = True |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 221 | self._work_queue.put(None) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 222 | if wait: |
| 223 | for t in self._threads: |
| 224 | t.join() |
| 225 | shutdown.__doc__ = _base.Executor.shutdown.__doc__ |