Skip to content

Commit f3c3d07

Browse files
committed
bpo-36780: Add wait_at_exit to ThreadPoolExecutor.shutdown.
1 parent 79972f1 commit f3c3d07

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

Doc/library/concurrent.futures.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,23 @@ And::
159159
.. versionchanged:: 3.7
160160
Added the *initializer* and *initargs* arguments.
161161

162+
.. method:: shutdown(wait=True, wait_at_exit=True)
163+
164+
Like :meth:`Executor.shutdown`, with the additional option to
165+
suppress waiting for running futures at Python exit.
166+
167+
Normally, this method waits for all submitted jobs to finish
168+
before returning. Specifying ``wait=False`` makes the method
169+
return immediately, but when the Python process is about to
170+
exit, it will still wait for the remaining futures to complete.
171+
172+
Specifying ``False`` for *wait_at_exit* inhibits waiting at
173+
process exit. It is typically used along with ``wait=False``,
174+
so ``shutdown(wait=False, wait_at_exit=False)`` abandons the
175+
executor and its futures. This is useful when the submitted
176+
jobs are possibly stuck, and waiting for them would make the
177+
process hang.
178+
162179

163180
.. _threadpoolexecutor-example:
164181

Lib/concurrent/futures/thread.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,14 @@ def _initializer_failed(self):
211211
if work_item is not None:
212212
work_item.future.set_exception(BrokenThreadPool(self._broken))
213213

214-
def shutdown(self, wait=True):
214+
def shutdown(self, wait=True, wait_at_exit=True):
215215
with self._shutdown_lock:
216216
self._shutdown = True
217217
self._work_queue.put(None)
218218
if wait:
219219
for t in self._threads:
220220
t.join()
221+
if not wait_at_exit:
222+
for t in self._threads:
223+
_threads_queues.pop(t, None)
221224
shutdown.__doc__ = _base.Executor.shutdown.__doc__

0 commit comments

Comments
 (0)