aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/asyncio/tasks.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2022-02-15 15:42:04 -0800
committerGitHub <noreply@github.com>2022-02-15 15:42:04 -0800
commit602630ac1855e38ef06361c68f6e216375a06180 (patch)
tree58a3f509fd92945d2676b0030c251713461460b6 /Lib/asyncio/tasks.py
parent08ec80113b3b7f7a9eaa3d217494536b63305181 (diff)
downloadcpython-602630ac1855e38ef06361c68f6e216375a06180.tar.gz
cpython-602630ac1855e38ef06361c68f6e216375a06180.zip
bpo-46752: Add TaskGroup; add Task..cancelled(),.uncancel() (GH-31270)
asyncio/taskgroups.py is an adaptation of taskgroup.py from EdgeDb, with the following key changes: - Allow creating new tasks as long as the last task hasn't finished - Raise [Base]ExceptionGroup (directly) rather than TaskGroupError deriving from MultiError - Instead of monkey-patching the parent task's cancel() method, add a new public API to Task The Task class has a new internal flag, `_cancel_requested`, which is set when `.cancel()` is called successfully. The `.cancelling()` method returns the value of this flag. Further `.cancel()` calls while this flag is set return False. To reset this flag, call `.uncancel()`. Thus, a Task that catches and ignores `CancelledError` should call `.uncancel()` if it wants to be cancellable again; until it does so, it is deemed to be busy with uninterruptible cleanup. This new Task API helps solve the problem where TaskGroup needs to distinguish between whether the parent task being cancelled "from the outside" vs. "from inside". Co-authored-by: Yury Selivanov <yury@edgedb.com> Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
Diffstat (limited to 'Lib/asyncio/tasks.py')
-rw-r--r--Lib/asyncio/tasks.py16
1 files changed, 15 insertions, 1 deletions
diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py
index 2bee5c050de..c11d0daaefe 100644
--- a/Lib/asyncio/tasks.py
+++ b/Lib/asyncio/tasks.py
@@ -105,6 +105,7 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
else:
self._name = str(name)
+ self._cancel_requested = False
self._must_cancel = False
self._fut_waiter = None
self._coro = coro
@@ -201,6 +202,9 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
self._log_traceback = False
if self.done():
return False
+ if self._cancel_requested:
+ return False
+ self._cancel_requested = True
if self._fut_waiter is not None:
if self._fut_waiter.cancel(msg=msg):
# Leave self._fut_waiter; it may be a Task that
@@ -212,6 +216,16 @@ class Task(futures._PyFuture): # Inherit Python Task implementation
self._cancel_message = msg
return True
+ def cancelling(self):
+ return self._cancel_requested
+
+ def uncancel(self):
+ if self._cancel_requested:
+ self._cancel_requested = False
+ return True
+ else:
+ return False
+
def __step(self, exc=None):
if self.done():
raise exceptions.InvalidStateError(
@@ -634,7 +648,7 @@ def _ensure_future(coro_or_future, *, loop=None):
loop = events._get_event_loop(stacklevel=4)
try:
return loop.create_task(coro_or_future)
- except RuntimeError:
+ except RuntimeError:
if not called_wrap_awaitable:
coro_or_future.close()
raise