Skip to content

Commit 474d51d

Browse files
committed
Support the cancel_futures parameter to executor.shutdown()
1 parent 608b713 commit 474d51d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/qasync/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,17 @@ def submit(self, callback, *args, **kwargs):
216216
def map(self, func, *iterables, timeout=None):
217217
raise NotImplementedError("use as_completed on the event loop")
218218

219-
def shutdown(self, wait=True):
219+
def shutdown(self, wait=True, *, cancel_futures=False):
220220
with self.__shutdown_lock:
221221
self.__been_shutdown = True
222222
self._logger.debug("Shutting down")
223+
if cancel_futures:
224+
# pop all the futures and cancel them
225+
while not self.__queue.empty():
226+
item = self.__queue.get_nowait()
227+
if item is not None:
228+
future, _, _, _ = item
229+
future.cancel()
223230
for i in range(len(self.__workers)):
224231
# Signal workers to stop
225232
self.__queue.put(None)

tests/test_qthreadexec.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
# BSD License
55
import logging
66
import threading
7+
import time
78
import weakref
9+
from concurrent.futures import CancelledError
810

911
import pytest
1012

@@ -140,3 +142,28 @@ def test_context(executor):
140142

141143
with pytest.raises(RuntimeError):
142144
executor.submit(lambda: 42)
145+
146+
147+
@pytest.mark.parametrize("cancel", [True, False])
148+
def test_shutdown_cancel_futures(executor, cancel):
149+
"""Test that shutdown with cancel_futures=True cancels all remaining futures in the queue."""
150+
151+
def task():
152+
time.sleep(0.01)
153+
154+
# Submit ten tasks to the executor
155+
futures = [executor.submit(task) for _ in range(10)]
156+
# shut it down
157+
executor.shutdown(cancel_futures=cancel)
158+
159+
cancels = 0
160+
for future in futures:
161+
try:
162+
future.result(timeout=0.01)
163+
except CancelledError:
164+
cancels += 1
165+
166+
if cancel:
167+
assert cancels > 0
168+
else:
169+
assert cancels == 0

0 commit comments

Comments
 (0)