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 ProcessPoolExecutor. |
| 5 | |
Thomas Grainger | f938d8b | 2019-04-12 17:17:17 +0100 | [diff] [blame] | 6 | The following diagram and text describe the data-flow through the system: |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 7 | |
| 8 | |======================= In-process =====================|== Out-of-process ==| |
| 9 | |
| 10 | +----------+ +----------+ +--------+ +-----------+ +---------+ |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 11 | | | => | Work Ids | | | | Call Q | | Process | |
| 12 | | | +----------+ | | +-----------+ | Pool | |
| 13 | | | | ... | | | | ... | +---------+ |
| 14 | | | | 6 | => | | => | 5, call() | => | | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 15 | | | | 7 | | | | ... | | | |
| 16 | | Process | | ... | | Local | +-----------+ | Process | |
| 17 | | Pool | +----------+ | Worker | | #1..n | |
| 18 | | Executor | | Thread | | | |
| 19 | | | +----------- + | | +-----------+ | | |
| 20 | | | <=> | Work Items | <=> | | <= | Result Q | <= | | |
| 21 | | | +------------+ | | +-----------+ | | |
| 22 | | | | 6: call() | | | | ... | | | |
| 23 | | | | future | | | | 4, result | | | |
| 24 | | | | ... | | | | 3, except | | | |
| 25 | +----------+ +------------+ +--------+ +-----------+ +---------+ |
| 26 | |
| 27 | Executor.submit() called: |
| 28 | - creates a uniquely numbered _WorkItem and adds it to the "Work Items" dict |
| 29 | - adds the id of the _WorkItem to the "Work Ids" queue |
| 30 | |
| 31 | Local worker thread: |
| 32 | - reads work ids from the "Work Ids" queue and looks up the corresponding |
| 33 | WorkItem from the "Work Items" dict: if the work item has been cancelled then |
| 34 | it is simply removed from the dict, otherwise it is repackaged as a |
| 35 | _CallItem and put in the "Call Q". New _CallItems are put in the "Call Q" |
| 36 | until "Call Q" is full. NOTE: the size of the "Call Q" is kept small because |
| 37 | calls placed in the "Call Q" can no longer be cancelled with Future.cancel(). |
| 38 | - reads _ResultItems from "Result Q", updates the future stored in the |
| 39 | "Work Items" dict and deletes the dict entry |
| 40 | |
| 41 | Process #1..n: |
| 42 | - reads _CallItems from "Call Q", executes the calls, and puts the resulting |
Mark Dickinson | 5ee2404 | 2012-10-20 13:16:49 +0100 | [diff] [blame] | 43 | _ResultItems in "Result Q" |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 44 | """ |
| 45 | |
| 46 | __author__ = 'Brian Quinlan (brian@sweetapp.com)' |
| 47 | |
| 48 | import atexit |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 49 | import os |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 50 | from concurrent.futures import _base |
| 51 | import queue |
Richard Oudkerk | 1f2eaa9 | 2013-10-16 17:06:22 +0100 | [diff] [blame] | 52 | from queue import Full |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 53 | import multiprocessing as mp |
Brian Quinlan | f7bda5c | 2019-05-07 13:31:11 -0400 | [diff] [blame] | 54 | import multiprocessing.connection |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 55 | from multiprocessing.queues import Queue |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 56 | import threading |
| 57 | import weakref |
Antoine Pitrou | 4aae276 | 2014-10-04 20:20:10 +0200 | [diff] [blame] | 58 | from functools import partial |
| 59 | import itertools |
Brian Quinlan | 3988986 | 2019-05-08 14:04:53 -0400 | [diff] [blame] | 60 | import sys |
Antoine Pitrou | 1285c9b | 2015-01-17 20:02:14 +0100 | [diff] [blame] | 61 | import traceback |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 62 | |
| 63 | # Workers are created as daemon threads and processes. This is done to allow the |
| 64 | # interpreter to exit when there are still idle processes in a |
| 65 | # ProcessPoolExecutor's process pool (i.e. shutdown() was not called). However, |
| 66 | # allowing workers to die with the interpreter has two undesirable properties: |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 67 | # - The workers would still be running during interpreter shutdown, |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 68 | # meaning that they would fail in unpredictable ways. |
| 69 | # - The workers could be killed while evaluating a work item, which could |
| 70 | # be bad if the callable being evaluated has external side-effects e.g. |
| 71 | # writing to a file. |
| 72 | # |
| 73 | # To work around this problem, an exit handler is installed which tells the |
| 74 | # workers to exit when their work queues are empty and then waits until the |
| 75 | # threads/processes finish. |
| 76 | |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 77 | _threads_wakeups = weakref.WeakKeyDictionary() |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 78 | _global_shutdown = False |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 79 | |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 80 | |
| 81 | class _ThreadWakeup: |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 82 | def __init__(self): |
| 83 | self._reader, self._writer = mp.Pipe(duplex=False) |
| 84 | |
Thomas Moreau | 095ee41 | 2018-03-12 18:18:41 +0100 | [diff] [blame] | 85 | def close(self): |
| 86 | self._writer.close() |
| 87 | self._reader.close() |
| 88 | |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 89 | def wakeup(self): |
| 90 | self._writer.send_bytes(b"") |
| 91 | |
| 92 | def clear(self): |
| 93 | while self._reader.poll(): |
| 94 | self._reader.recv_bytes() |
| 95 | |
| 96 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 97 | def _python_exit(): |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 98 | global _global_shutdown |
| 99 | _global_shutdown = True |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 100 | items = list(_threads_wakeups.items()) |
| 101 | for _, thread_wakeup in items: |
| 102 | thread_wakeup.wakeup() |
| 103 | for t, _ in items: |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 104 | t.join() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 105 | |
| 106 | # Controls how many more calls than processes will be queued in the call queue. |
| 107 | # A smaller number will mean that processes spend more time idle waiting for |
| 108 | # work while a larger number will make Future.cancel() succeed less frequently |
| 109 | # (Futures in the call queue cannot be cancelled). |
| 110 | EXTRA_QUEUED_CALLS = 1 |
| 111 | |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 112 | |
Brian Quinlan | 3988986 | 2019-05-08 14:04:53 -0400 | [diff] [blame] | 113 | # On Windows, WaitForMultipleObjects is used to wait for processes to finish. |
| 114 | # It can wait on, at most, 63 objects. There is an overhead of two objects: |
| 115 | # - the result queue reader |
| 116 | # - the thread wakeup reader |
| 117 | _MAX_WINDOWS_WORKERS = 63 - 2 |
| 118 | |
Antoine Pitrou | 1285c9b | 2015-01-17 20:02:14 +0100 | [diff] [blame] | 119 | # Hack to embed stringification of remote traceback in local traceback |
| 120 | |
| 121 | class _RemoteTraceback(Exception): |
| 122 | def __init__(self, tb): |
| 123 | self.tb = tb |
| 124 | def __str__(self): |
| 125 | return self.tb |
| 126 | |
| 127 | class _ExceptionWithTraceback: |
| 128 | def __init__(self, exc, tb): |
| 129 | tb = traceback.format_exception(type(exc), exc, tb) |
| 130 | tb = ''.join(tb) |
| 131 | self.exc = exc |
| 132 | self.tb = '\n"""\n%s"""' % tb |
| 133 | def __reduce__(self): |
| 134 | return _rebuild_exc, (self.exc, self.tb) |
| 135 | |
| 136 | def _rebuild_exc(exc, tb): |
| 137 | exc.__cause__ = _RemoteTraceback(tb) |
| 138 | return exc |
| 139 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 140 | class _WorkItem(object): |
| 141 | def __init__(self, future, fn, args, kwargs): |
| 142 | self.future = future |
| 143 | self.fn = fn |
| 144 | self.args = args |
| 145 | self.kwargs = kwargs |
| 146 | |
| 147 | class _ResultItem(object): |
| 148 | def __init__(self, work_id, exception=None, result=None): |
| 149 | self.work_id = work_id |
| 150 | self.exception = exception |
| 151 | self.result = result |
| 152 | |
| 153 | class _CallItem(object): |
| 154 | def __init__(self, work_id, fn, args, kwargs): |
| 155 | self.work_id = work_id |
| 156 | self.fn = fn |
| 157 | self.args = args |
| 158 | self.kwargs = kwargs |
| 159 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 160 | |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 161 | class _SafeQueue(Queue): |
| 162 | """Safe Queue set exception to the future object linked to a job""" |
| 163 | def __init__(self, max_size=0, *, ctx, pending_work_items): |
| 164 | self.pending_work_items = pending_work_items |
| 165 | super().__init__(max_size, ctx=ctx) |
| 166 | |
| 167 | def _on_queue_feeder_error(self, e, obj): |
| 168 | if isinstance(obj, _CallItem): |
| 169 | tb = traceback.format_exception(type(e), e, e.__traceback__) |
| 170 | e.__cause__ = _RemoteTraceback('\n"""\n{}"""'.format(''.join(tb))) |
| 171 | work_item = self.pending_work_items.pop(obj.work_id, None) |
| 172 | # work_item can be None if another process terminated. In this case, |
| 173 | # the queue_manager_thread fails all work_items with BrokenProcessPool |
| 174 | if work_item is not None: |
| 175 | work_item.future.set_exception(e) |
| 176 | else: |
| 177 | super()._on_queue_feeder_error(e, obj) |
| 178 | |
| 179 | |
Antoine Pitrou | 4aae276 | 2014-10-04 20:20:10 +0200 | [diff] [blame] | 180 | def _get_chunks(*iterables, chunksize): |
| 181 | """ Iterates over zip()ed iterables in chunks. """ |
| 182 | it = zip(*iterables) |
| 183 | while True: |
| 184 | chunk = tuple(itertools.islice(it, chunksize)) |
| 185 | if not chunk: |
| 186 | return |
| 187 | yield chunk |
| 188 | |
| 189 | def _process_chunk(fn, chunk): |
| 190 | """ Processes a chunk of an iterable passed to map. |
| 191 | |
| 192 | Runs the function passed to map() on a chunk of the |
| 193 | iterable passed to map. |
| 194 | |
| 195 | This function is run in a separate process. |
| 196 | |
| 197 | """ |
| 198 | return [fn(*args) for args in chunk] |
| 199 | |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 200 | |
| 201 | def _sendback_result(result_queue, work_id, result=None, exception=None): |
| 202 | """Safely send back the given result or exception""" |
| 203 | try: |
| 204 | result_queue.put(_ResultItem(work_id, result=result, |
| 205 | exception=exception)) |
| 206 | except BaseException as e: |
| 207 | exc = _ExceptionWithTraceback(e, e.__traceback__) |
| 208 | result_queue.put(_ResultItem(work_id, exception=exc)) |
| 209 | |
| 210 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 211 | def _process_worker(call_queue, result_queue, initializer, initargs): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 212 | """Evaluates calls from call_queue and places the results in result_queue. |
| 213 | |
Georg Brandl | fb1720b | 2010-12-09 18:08:43 +0000 | [diff] [blame] | 214 | This worker is run in a separate process. |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 215 | |
| 216 | Args: |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 217 | call_queue: A ctx.Queue of _CallItems that will be read and |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 218 | evaluated by the worker. |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 219 | result_queue: A ctx.Queue of _ResultItems that will written |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 220 | to by the worker. |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 221 | initializer: A callable initializer, or None |
| 222 | initargs: A tuple of args for the initializer |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 223 | """ |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 224 | if initializer is not None: |
| 225 | try: |
| 226 | initializer(*initargs) |
| 227 | except BaseException: |
| 228 | _base.LOGGER.critical('Exception in initializer:', exc_info=True) |
| 229 | # The parent will notice that the process stopped and |
| 230 | # mark the pool broken |
| 231 | return |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 232 | while True: |
Antoine Pitrou | 27be5da | 2011-04-12 17:48:46 +0200 | [diff] [blame] | 233 | call_item = call_queue.get(block=True) |
| 234 | if call_item is None: |
| 235 | # Wake up queue management thread |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 236 | result_queue.put(os.getpid()) |
Antoine Pitrou | 27be5da | 2011-04-12 17:48:46 +0200 | [diff] [blame] | 237 | return |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 238 | try: |
Antoine Pitrou | 27be5da | 2011-04-12 17:48:46 +0200 | [diff] [blame] | 239 | r = call_item.fn(*call_item.args, **call_item.kwargs) |
| 240 | except BaseException as e: |
Antoine Pitrou | 1285c9b | 2015-01-17 20:02:14 +0100 | [diff] [blame] | 241 | exc = _ExceptionWithTraceback(e, e.__traceback__) |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 242 | _sendback_result(result_queue, call_item.work_id, exception=exc) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 243 | else: |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 244 | _sendback_result(result_queue, call_item.work_id, result=r) |
Dave Chevell | 962bdea | 2019-03-17 09:28:51 +1100 | [diff] [blame] | 245 | del r |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 246 | |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 247 | # Liberate the resource as soon as possible, to avoid holding onto |
| 248 | # open files or shared memory that is not needed anymore |
| 249 | del call_item |
| 250 | |
| 251 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 252 | def _add_call_item_to_queue(pending_work_items, |
| 253 | work_ids, |
| 254 | call_queue): |
| 255 | """Fills call_queue with _WorkItems from pending_work_items. |
| 256 | |
| 257 | This function never blocks. |
| 258 | |
| 259 | Args: |
| 260 | pending_work_items: A dict mapping work ids to _WorkItems e.g. |
| 261 | {5: <_WorkItem...>, 6: <_WorkItem...>, ...} |
| 262 | work_ids: A queue.Queue of work ids e.g. Queue([5, 6, ...]). Work ids |
| 263 | are consumed and the corresponding _WorkItems from |
| 264 | pending_work_items are transformed into _CallItems and put in |
| 265 | call_queue. |
| 266 | call_queue: A multiprocessing.Queue that will be filled with _CallItems |
| 267 | derived from _WorkItems. |
| 268 | """ |
| 269 | while True: |
| 270 | if call_queue.full(): |
| 271 | return |
| 272 | try: |
| 273 | work_id = work_ids.get(block=False) |
| 274 | except queue.Empty: |
| 275 | return |
| 276 | else: |
| 277 | work_item = pending_work_items[work_id] |
| 278 | |
| 279 | if work_item.future.set_running_or_notify_cancel(): |
| 280 | call_queue.put(_CallItem(work_id, |
| 281 | work_item.fn, |
| 282 | work_item.args, |
| 283 | work_item.kwargs), |
| 284 | block=True) |
| 285 | else: |
| 286 | del pending_work_items[work_id] |
| 287 | continue |
| 288 | |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 289 | |
Antoine Pitrou | b87a56a | 2011-05-03 16:34:42 +0200 | [diff] [blame] | 290 | def _queue_management_worker(executor_reference, |
| 291 | processes, |
| 292 | pending_work_items, |
| 293 | work_ids_queue, |
| 294 | call_queue, |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 295 | result_queue, |
| 296 | thread_wakeup): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 297 | """Manages the communication between this process and the worker processes. |
| 298 | |
| 299 | This function is run in a local thread. |
| 300 | |
| 301 | Args: |
| 302 | executor_reference: A weakref.ref to the ProcessPoolExecutor that owns |
| 303 | this thread. Used to determine if the ProcessPoolExecutor has been |
| 304 | garbage collected and that this function can exit. |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 305 | process: A list of the ctx.Process instances used as |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 306 | workers. |
| 307 | pending_work_items: A dict mapping work ids to _WorkItems e.g. |
| 308 | {5: <_WorkItem...>, 6: <_WorkItem...>, ...} |
| 309 | work_ids_queue: A queue.Queue of work ids e.g. Queue([5, 6, ...]). |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 310 | call_queue: A ctx.Queue that will be filled with _CallItems |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 311 | derived from _WorkItems for processing by the process workers. |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 312 | result_queue: A ctx.SimpleQueue of _ResultItems generated by the |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 313 | process workers. |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 314 | thread_wakeup: A _ThreadWakeup to allow waking up the |
| 315 | queue_manager_thread from the main Thread and avoid deadlocks |
| 316 | caused by permanently locked queues. |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 317 | """ |
Antoine Pitrou | 020436b | 2011-07-02 21:20:25 +0200 | [diff] [blame] | 318 | executor = None |
| 319 | |
| 320 | def shutting_down(): |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 321 | return (_global_shutdown or executor is None |
| 322 | or executor._shutdown_thread) |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 323 | |
| 324 | def shutdown_worker(): |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 325 | # This is an upper bound on the number of children alive. |
| 326 | n_children_alive = sum(p.is_alive() for p in processes.values()) |
| 327 | n_children_to_stop = n_children_alive |
| 328 | n_sentinels_sent = 0 |
| 329 | # Send the right number of sentinels, to make sure all children are |
| 330 | # properly terminated. |
| 331 | while n_sentinels_sent < n_children_to_stop and n_children_alive > 0: |
| 332 | for i in range(n_children_to_stop - n_sentinels_sent): |
| 333 | try: |
| 334 | call_queue.put_nowait(None) |
| 335 | n_sentinels_sent += 1 |
| 336 | except Full: |
| 337 | break |
| 338 | n_children_alive = sum(p.is_alive() for p in processes.values()) |
| 339 | |
Antoine Pitrou | dc19c24 | 2011-07-16 01:51:58 +0200 | [diff] [blame] | 340 | # Release the queue's resources as soon as possible. |
| 341 | call_queue.close() |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 342 | # If .join() is not called on the created processes then |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 343 | # some ctx.Queue methods may deadlock on Mac OS X. |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 344 | for p in processes.values(): |
| 345 | p.join() |
| 346 | |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 347 | result_reader = result_queue._reader |
| 348 | wakeup_reader = thread_wakeup._reader |
| 349 | readers = [result_reader, wakeup_reader] |
Antoine Pitrou | bdb1cf1 | 2012-03-05 19:28:37 +0100 | [diff] [blame] | 350 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 351 | while True: |
| 352 | _add_call_item_to_queue(pending_work_items, |
| 353 | work_ids_queue, |
| 354 | call_queue) |
| 355 | |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 356 | # Wait for a result to be ready in the result_queue while checking |
| 357 | # that all worker processes are still running, or for a wake up |
| 358 | # signal send. The wake up signals come either from new tasks being |
| 359 | # submitted, from the executor being shutdown/gc-ed, or from the |
| 360 | # shutdown of the python interpreter. |
| 361 | worker_sentinels = [p.sentinel for p in processes.values()] |
Brian Quinlan | f7bda5c | 2019-05-07 13:31:11 -0400 | [diff] [blame] | 362 | ready = mp.connection.wait(readers + worker_sentinels) |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 363 | |
| 364 | cause = None |
| 365 | is_broken = True |
| 366 | if result_reader in ready: |
| 367 | try: |
| 368 | result_item = result_reader.recv() |
| 369 | is_broken = False |
| 370 | except BaseException as e: |
| 371 | cause = traceback.format_exception(type(e), e, e.__traceback__) |
| 372 | |
| 373 | elif wakeup_reader in ready: |
| 374 | is_broken = False |
| 375 | result_item = None |
| 376 | thread_wakeup.clear() |
| 377 | if is_broken: |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 378 | # Mark the process pool broken so that submits fail right now. |
| 379 | executor = executor_reference() |
| 380 | if executor is not None: |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 381 | executor._broken = ('A child process terminated ' |
| 382 | 'abruptly, the process pool is not ' |
| 383 | 'usable anymore') |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 384 | executor._shutdown_thread = True |
Antoine Pitrou | 020436b | 2011-07-02 21:20:25 +0200 | [diff] [blame] | 385 | executor = None |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 386 | bpe = BrokenProcessPool("A process in the process pool was " |
| 387 | "terminated abruptly while the future was " |
| 388 | "running or pending.") |
| 389 | if cause is not None: |
| 390 | bpe.__cause__ = _RemoteTraceback( |
| 391 | f"\n'''\n{''.join(cause)}'''") |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 392 | # All futures in flight must be marked failed |
| 393 | for work_id, work_item in pending_work_items.items(): |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 394 | work_item.future.set_exception(bpe) |
Andrew Svetlov | 6b97374 | 2012-11-03 15:36:01 +0200 | [diff] [blame] | 395 | # Delete references to object. See issue16284 |
| 396 | del work_item |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 397 | pending_work_items.clear() |
| 398 | # Terminate remaining workers forcibly: the queues or their |
| 399 | # locks may be in a dirty state and block forever. |
| 400 | for p in processes.values(): |
| 401 | p.terminate() |
Antoine Pitrou | dc19c24 | 2011-07-16 01:51:58 +0200 | [diff] [blame] | 402 | shutdown_worker() |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 403 | return |
| 404 | if isinstance(result_item, int): |
| 405 | # Clean shutdown of a worker using its PID |
| 406 | # (avoids marking the executor broken) |
Antoine Pitrou | 020436b | 2011-07-02 21:20:25 +0200 | [diff] [blame] | 407 | assert shutting_down() |
Antoine Pitrou | d06a065 | 2011-07-16 01:13:34 +0200 | [diff] [blame] | 408 | p = processes.pop(result_item) |
| 409 | p.join() |
Antoine Pitrou | 020436b | 2011-07-02 21:20:25 +0200 | [diff] [blame] | 410 | if not processes: |
| 411 | shutdown_worker() |
| 412 | return |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 413 | elif result_item is not None: |
| 414 | work_item = pending_work_items.pop(result_item.work_id, None) |
| 415 | # work_item can be None if another process terminated (see above) |
| 416 | if work_item is not None: |
| 417 | if result_item.exception: |
| 418 | work_item.future.set_exception(result_item.exception) |
| 419 | else: |
| 420 | work_item.future.set_result(result_item.result) |
Andrew Svetlov | 6b97374 | 2012-11-03 15:36:01 +0200 | [diff] [blame] | 421 | # Delete references to object. See issue16284 |
| 422 | del work_item |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 423 | # Delete reference to result_item |
| 424 | del result_item |
| 425 | |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 426 | # Check whether we should start shutting down. |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 427 | executor = executor_reference() |
| 428 | # No more work items can be added if: |
| 429 | # - The interpreter is shutting down OR |
| 430 | # - The executor that owns this worker has been collected OR |
| 431 | # - The executor that owns this worker has been shutdown. |
Antoine Pitrou | 020436b | 2011-07-02 21:20:25 +0200 | [diff] [blame] | 432 | if shutting_down(): |
Antoine Pitrou | 020436b | 2011-07-02 21:20:25 +0200 | [diff] [blame] | 433 | try: |
Mark Nemec | c4b695f | 2018-04-10 18:23:14 +0100 | [diff] [blame] | 434 | # Flag the executor as shutting down as early as possible if it |
| 435 | # is not gc-ed yet. |
| 436 | if executor is not None: |
| 437 | executor._shutdown_thread = True |
Kyle Stanley | 339fd46 | 2020-02-02 07:49:00 -0500 | [diff] [blame] | 438 | # Unless there are pending work items, we have nothing to cancel. |
| 439 | if pending_work_items and executor._cancel_pending_futures: |
| 440 | # Cancel all pending futures and update pending_work_items |
| 441 | # to only have futures that are currently running. |
| 442 | new_pending_work_items = {} |
| 443 | for work_id, work_item in pending_work_items.items(): |
| 444 | if not work_item.future.cancel(): |
| 445 | new_pending_work_items[work_id] = work_item |
| 446 | |
| 447 | pending_work_items = new_pending_work_items |
| 448 | # Drain work_ids_queue since we no longer need to |
| 449 | # add items to the call queue. |
| 450 | while True: |
| 451 | try: |
| 452 | work_ids_queue.get_nowait() |
| 453 | except queue.Empty: |
| 454 | break |
| 455 | |
Antoine Pitrou | 1c405b3 | 2011-07-03 13:17:06 +0200 | [diff] [blame] | 456 | # Since no new work items can be added, it is safe to shutdown |
| 457 | # this thread if there are no pending work items. |
| 458 | if not pending_work_items: |
| 459 | shutdown_worker() |
| 460 | return |
Antoine Pitrou | 020436b | 2011-07-02 21:20:25 +0200 | [diff] [blame] | 461 | except Full: |
| 462 | # This is not a problem: we will eventually be woken up (in |
Antoine Pitrou | 1c405b3 | 2011-07-03 13:17:06 +0200 | [diff] [blame] | 463 | # result_queue.get()) and be able to send a sentinel again. |
Antoine Pitrou | 020436b | 2011-07-02 21:20:25 +0200 | [diff] [blame] | 464 | pass |
| 465 | executor = None |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 466 | |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 467 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 468 | _system_limits_checked = False |
| 469 | _system_limited = None |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 470 | |
| 471 | |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 472 | def _check_system_limits(): |
| 473 | global _system_limits_checked, _system_limited |
| 474 | if _system_limits_checked: |
| 475 | if _system_limited: |
| 476 | raise NotImplementedError(_system_limited) |
| 477 | _system_limits_checked = True |
| 478 | try: |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 479 | nsems_max = os.sysconf("SC_SEM_NSEMS_MAX") |
| 480 | except (AttributeError, ValueError): |
| 481 | # sysconf not available or setting not available |
| 482 | return |
| 483 | if nsems_max == -1: |
Ezio Melotti | b5bc353 | 2013-08-17 16:11:40 +0300 | [diff] [blame] | 484 | # indetermined limit, assume that limit is determined |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 485 | # by available memory only |
| 486 | return |
| 487 | if nsems_max >= 256: |
| 488 | # minimum number of semaphores available |
| 489 | # according to POSIX |
| 490 | return |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 491 | _system_limited = ("system provides too few semaphores (%d" |
| 492 | " available, 256 necessary)" % nsems_max) |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 493 | raise NotImplementedError(_system_limited) |
| 494 | |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 495 | |
Grzegorz Grzywacz | 97e1b1c | 2017-09-01 18:54:00 +0200 | [diff] [blame] | 496 | def _chain_from_iterable_of_lists(iterable): |
| 497 | """ |
| 498 | Specialized implementation of itertools.chain.from_iterable. |
| 499 | Each item in *iterable* should be a list. This function is |
| 500 | careful not to keep references to yielded objects. |
| 501 | """ |
| 502 | for element in iterable: |
| 503 | element.reverse() |
| 504 | while element: |
| 505 | yield element.pop() |
| 506 | |
| 507 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 508 | class BrokenProcessPool(_base.BrokenExecutor): |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 509 | """ |
| 510 | Raised when a process in a ProcessPoolExecutor terminated abruptly |
| 511 | while a future was in the running state. |
| 512 | """ |
| 513 | |
| 514 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 515 | class ProcessPoolExecutor(_base.Executor): |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 516 | def __init__(self, max_workers=None, mp_context=None, |
| 517 | initializer=None, initargs=()): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 518 | """Initializes a new ProcessPoolExecutor instance. |
| 519 | |
| 520 | Args: |
| 521 | max_workers: The maximum number of processes that can be used to |
| 522 | execute the given calls. If None or not given then as many |
| 523 | worker processes will be created as the machine has processors. |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 524 | mp_context: A multiprocessing context to launch the workers. This |
| 525 | object should provide SimpleQueue, Queue and Process. |
ubordignon | 552ace7 | 2019-06-15 13:43:10 +0200 | [diff] [blame] | 526 | initializer: A callable used to initialize worker processes. |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 527 | initargs: A tuple of arguments to pass to the initializer. |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 528 | """ |
Martin v. Löwis | 9f6d48b | 2011-01-03 00:07:01 +0000 | [diff] [blame] | 529 | _check_system_limits() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 530 | |
| 531 | if max_workers is None: |
Charles-François Natali | 37cfb0a | 2013-06-28 19:25:45 +0200 | [diff] [blame] | 532 | self._max_workers = os.cpu_count() or 1 |
Brian Quinlan | 3988986 | 2019-05-08 14:04:53 -0400 | [diff] [blame] | 533 | if sys.platform == 'win32': |
| 534 | self._max_workers = min(_MAX_WINDOWS_WORKERS, |
| 535 | self._max_workers) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 536 | else: |
Brian Quinlan | 20efceb | 2014-05-17 13:51:10 -0700 | [diff] [blame] | 537 | if max_workers <= 0: |
| 538 | raise ValueError("max_workers must be greater than 0") |
Brian Quinlan | 3988986 | 2019-05-08 14:04:53 -0400 | [diff] [blame] | 539 | elif (sys.platform == 'win32' and |
| 540 | max_workers > _MAX_WINDOWS_WORKERS): |
| 541 | raise ValueError( |
| 542 | f"max_workers must be <= {_MAX_WINDOWS_WORKERS}") |
Brian Quinlan | 20efceb | 2014-05-17 13:51:10 -0700 | [diff] [blame] | 543 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 544 | self._max_workers = max_workers |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 545 | |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 546 | if mp_context is None: |
| 547 | mp_context = mp.get_context() |
| 548 | self._mp_context = mp_context |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 549 | |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 550 | if initializer is not None and not callable(initializer): |
| 551 | raise TypeError("initializer must be a callable") |
| 552 | self._initializer = initializer |
| 553 | self._initargs = initargs |
| 554 | |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 555 | # Management thread |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 556 | self._queue_management_thread = None |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 557 | |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 558 | # Map of pids to processes |
| 559 | self._processes = {} |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 560 | |
| 561 | # Shutdown is a two-step process. |
| 562 | self._shutdown_thread = False |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 563 | self._shutdown_lock = threading.Lock() |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 564 | self._broken = False |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 565 | self._queue_count = 0 |
| 566 | self._pending_work_items = {} |
Kyle Stanley | 339fd46 | 2020-02-02 07:49:00 -0500 | [diff] [blame] | 567 | self._cancel_pending_futures = False |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 568 | |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 569 | # Create communication channels for the executor |
| 570 | # Make the call queue slightly larger than the number of processes to |
| 571 | # prevent the worker processes from idling. But don't make it too big |
| 572 | # because futures in the call queue cannot be cancelled. |
| 573 | queue_size = self._max_workers + EXTRA_QUEUED_CALLS |
| 574 | self._call_queue = _SafeQueue( |
| 575 | max_size=queue_size, ctx=self._mp_context, |
| 576 | pending_work_items=self._pending_work_items) |
| 577 | # Killed worker processes can produce spurious "broken pipe" |
| 578 | # tracebacks in the queue's own worker thread. But we detect killed |
| 579 | # processes anyway, so silence the tracebacks. |
| 580 | self._call_queue._ignore_epipe = True |
| 581 | self._result_queue = mp_context.SimpleQueue() |
| 582 | self._work_ids = queue.Queue() |
| 583 | |
| 584 | # _ThreadWakeup is a communication channel used to interrupt the wait |
| 585 | # of the main loop of queue_manager_thread from another thread (e.g. |
| 586 | # when calling executor.submit or executor.shutdown). We do not use the |
| 587 | # _result_queue to send the wakeup signal to the queue_manager_thread |
| 588 | # as it could result in a deadlock if a worker process dies with the |
| 589 | # _result_queue write lock still acquired. |
| 590 | self._queue_management_thread_wakeup = _ThreadWakeup() |
| 591 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 592 | def _start_queue_management_thread(self): |
| 593 | if self._queue_management_thread is None: |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 594 | # When the executor gets garbarge collected, the weakref callback |
| 595 | # will wake up the queue management thread so that it can terminate |
| 596 | # if there is no pending work item. |
| 597 | def weakref_cb(_, |
| 598 | thread_wakeup=self._queue_management_thread_wakeup): |
| 599 | mp.util.debug('Executor collected: triggering callback for' |
| 600 | ' QueueManager wakeup') |
| 601 | thread_wakeup.wakeup() |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 602 | # Start the processes so that their sentinels are known. |
| 603 | self._adjust_process_count() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 604 | self._queue_management_thread = threading.Thread( |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 605 | target=_queue_management_worker, |
| 606 | args=(weakref.ref(self, weakref_cb), |
| 607 | self._processes, |
| 608 | self._pending_work_items, |
| 609 | self._work_ids, |
| 610 | self._call_queue, |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 611 | self._result_queue, |
| 612 | self._queue_management_thread_wakeup), |
| 613 | name="QueueManagerThread") |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 614 | self._queue_management_thread.daemon = True |
| 615 | self._queue_management_thread.start() |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 616 | _threads_wakeups[self._queue_management_thread] = \ |
| 617 | self._queue_management_thread_wakeup |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 618 | |
| 619 | def _adjust_process_count(self): |
| 620 | for _ in range(len(self._processes), self._max_workers): |
Thomas Moreau | e8c368d | 2017-10-03 11:53:17 +0200 | [diff] [blame] | 621 | p = self._mp_context.Process( |
| 622 | target=_process_worker, |
| 623 | args=(self._call_queue, |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 624 | self._result_queue, |
| 625 | self._initializer, |
| 626 | self._initargs)) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 627 | p.start() |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 628 | self._processes[p.pid] = p |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 629 | |
Serhiy Storchaka | 142566c | 2019-06-05 18:22:31 +0300 | [diff] [blame] | 630 | def submit(self, fn, /, *args, **kwargs): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 631 | with self._shutdown_lock: |
Antoine Pitrou | dd69649 | 2011-06-08 17:21:55 +0200 | [diff] [blame] | 632 | if self._broken: |
Antoine Pitrou | 63ff413 | 2017-11-04 11:05:49 +0100 | [diff] [blame] | 633 | raise BrokenProcessPool(self._broken) |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 634 | if self._shutdown_thread: |
| 635 | raise RuntimeError('cannot schedule new futures after shutdown') |
Mark Nemec | c4b695f | 2018-04-10 18:23:14 +0100 | [diff] [blame] | 636 | if _global_shutdown: |
| 637 | raise RuntimeError('cannot schedule new futures after ' |
| 638 | 'interpreter shutdown') |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 639 | |
| 640 | f = _base.Future() |
| 641 | w = _WorkItem(f, fn, args, kwargs) |
| 642 | |
| 643 | self._pending_work_items[self._queue_count] = w |
| 644 | self._work_ids.put(self._queue_count) |
| 645 | self._queue_count += 1 |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 646 | # Wake up queue management thread |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 647 | self._queue_management_thread_wakeup.wakeup() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 648 | |
| 649 | self._start_queue_management_thread() |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 650 | return f |
| 651 | submit.__doc__ = _base.Executor.submit.__doc__ |
| 652 | |
Antoine Pitrou | 4aae276 | 2014-10-04 20:20:10 +0200 | [diff] [blame] | 653 | def map(self, fn, *iterables, timeout=None, chunksize=1): |
Martin Panter | d2ad571 | 2015-11-02 04:20:33 +0000 | [diff] [blame] | 654 | """Returns an iterator equivalent to map(fn, iter). |
Antoine Pitrou | 4aae276 | 2014-10-04 20:20:10 +0200 | [diff] [blame] | 655 | |
| 656 | Args: |
| 657 | fn: A callable that will take as many arguments as there are |
| 658 | passed iterables. |
| 659 | timeout: The maximum number of seconds to wait. If None, then there |
| 660 | is no limit on the wait time. |
| 661 | chunksize: If greater than one, the iterables will be chopped into |
| 662 | chunks of size chunksize and submitted to the process pool. |
| 663 | If set to one, the items in the list will be sent one at a time. |
| 664 | |
| 665 | Returns: |
| 666 | An iterator equivalent to: map(func, *iterables) but the calls may |
| 667 | be evaluated out-of-order. |
| 668 | |
| 669 | Raises: |
| 670 | TimeoutError: If the entire result iterator could not be generated |
| 671 | before the given timeout. |
| 672 | Exception: If fn(*args) raises for any values. |
| 673 | """ |
| 674 | if chunksize < 1: |
| 675 | raise ValueError("chunksize must be >= 1.") |
| 676 | |
| 677 | results = super().map(partial(_process_chunk, fn), |
| 678 | _get_chunks(*iterables, chunksize=chunksize), |
| 679 | timeout=timeout) |
Grzegorz Grzywacz | 97e1b1c | 2017-09-01 18:54:00 +0200 | [diff] [blame] | 680 | return _chain_from_iterable_of_lists(results) |
Antoine Pitrou | 4aae276 | 2014-10-04 20:20:10 +0200 | [diff] [blame] | 681 | |
Kyle Stanley | 339fd46 | 2020-02-02 07:49:00 -0500 | [diff] [blame] | 682 | def shutdown(self, wait=True, *, cancel_futures=False): |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 683 | with self._shutdown_lock: |
Kyle Stanley | 339fd46 | 2020-02-02 07:49:00 -0500 | [diff] [blame] | 684 | self._cancel_pending_futures = cancel_futures |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 685 | self._shutdown_thread = True |
Kyle Stanley | 339fd46 | 2020-02-02 07:49:00 -0500 | [diff] [blame] | 686 | |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 687 | if self._queue_management_thread: |
| 688 | # Wake up queue management thread |
Thomas Moreau | 94459fd | 2018-01-05 11:15:54 +0100 | [diff] [blame] | 689 | self._queue_management_thread_wakeup.wakeup() |
Antoine Pitrou | c13d454 | 2011-03-26 19:29:44 +0100 | [diff] [blame] | 690 | if wait: |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 691 | self._queue_management_thread.join() |
Ezio Melotti | b5bc353 | 2013-08-17 16:11:40 +0300 | [diff] [blame] | 692 | # To reduce the risk of opening too many files, remove references to |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 693 | # objects that use file descriptors. |
| 694 | self._queue_management_thread = None |
Victor Stinner | b713adf | 2017-09-02 00:25:11 +0200 | [diff] [blame] | 695 | if self._call_queue is not None: |
| 696 | self._call_queue.close() |
| 697 | if wait: |
| 698 | self._call_queue.join_thread() |
| 699 | self._call_queue = None |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 700 | self._result_queue = None |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 701 | self._processes = None |
Thomas Moreau | 095ee41 | 2018-03-12 18:18:41 +0100 | [diff] [blame] | 702 | |
| 703 | if self._queue_management_thread_wakeup: |
| 704 | self._queue_management_thread_wakeup.close() |
| 705 | self._queue_management_thread_wakeup = None |
| 706 | |
Brian Quinlan | 81c4d36 | 2010-09-18 22:35:02 +0000 | [diff] [blame] | 707 | shutdown.__doc__ = _base.Executor.shutdown.__doc__ |
| 708 | |
| 709 | atexit.register(_python_exit) |