diff options
| author | Kyle Stanley <aeros167@gmail.com> | 2020-02-02 07:49:00 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-02 13:49:00 +0100 |
| commit | 339fd46cb764277cbbdc3e78dcc5b45b156bb6ae (patch) | |
| tree | 2366d3abf217d3017a50e2b024d67be731a49347 /Lib/concurrent/futures/thread.py | |
| parent | be8147bdc6111a225ec284a4514277304726c3d0 (diff) | |
| download | cpython-git-339fd46cb764277cbbdc3e78dcc5b45b156bb6ae.tar.gz | |
bpo-39349: Add *cancel_futures* to Executor.shutdown() (GH-18057)
Diffstat (limited to 'Lib/concurrent/futures/thread.py')
| -rw-r--r-- | Lib/concurrent/futures/thread.py | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/Lib/concurrent/futures/thread.py b/Lib/concurrent/futures/thread.py index b89f8f24d4..be79161bf8 100644 --- a/Lib/concurrent/futures/thread.py +++ b/Lib/concurrent/futures/thread.py @@ -215,9 +215,22 @@ class ThreadPoolExecutor(_base.Executor): if work_item is not None: work_item.future.set_exception(BrokenThreadPool(self._broken)) - def shutdown(self, wait=True): + def shutdown(self, wait=True, *, cancel_futures=False): with self._shutdown_lock: self._shutdown = True + if cancel_futures: + # Drain all work items from the queue, and then cancel their + # associated futures. + while True: + try: + work_item = self._work_queue.get_nowait() + except queue.Empty: + break + if work_item is not None: + work_item.future.cancel() + + # Send a wake-up to prevent threads calling + # _work_queue.get(block=True) from permanently blocking. self._work_queue.put(None) if wait: for t in self._threads: |
