Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d21357

Browse files
committed
Issue #21601: Document asyncio.Task.cancel(). Initial patch written by Vajrasky
Kok.
1 parent 66f2928 commit 8d21357

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

Doc/library/asyncio-task.rst

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,27 @@ Task
327327

328328
``None`` is returned when called not in the context of a :class:`Task`.
329329

330-
.. method:: get_stack(self, \*, limit=None)
330+
.. method:: cancel()
331+
332+
Request this task to cancel itself.
333+
334+
This arranges for a :exc:`~concurrent.futures.CancelledError` to be
335+
thrown into the wrapped coroutine on the next cycle through the event
336+
loop. The coroutine then has a chance to clean up or even deny the
337+
request using try/except/finally.
338+
339+
Contrary to :meth:`Future.cancel`, this does not guarantee that the task
340+
will be cancelled: the exception might be caught and acted upon, delaying
341+
cancellation of the task or preventing it completely. The task may also
342+
return a value or raise a different exception.
343+
344+
Immediately after this method is called, :meth:`~Future.cancelled` will
345+
not return ``True`` (unless the task was already cancelled). A task will
346+
be marked as cancelled when the wrapped coroutine terminates with a
347+
:exc:`~concurrent.futures.CancelledError` exception (even if
348+
:meth:`cancel` was not called).
349+
350+
.. method:: get_stack(\*, limit=None)
331351

332352
Return the list of stack frames for this task's coroutine.
333353

Lib/asyncio/tasks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ def print_stack(self, *, limit=None, file=None):
269269
print(line, file=file, end='')
270270

271271
def cancel(self):
272-
"""Request that a task to cancel itself.
272+
"""Request this task to cancel itself.
273273
274-
This arranges for a CancellationError to be thrown into the
274+
This arranges for a CancelledError to be thrown into the
275275
wrapped coroutine on the next cycle through the event loop.
276276
The coroutine then has a chance to clean up or even deny
277277
the request using try/except/finally.

0 commit comments

Comments
 (0)