On this page
asyncio.CancelledError
Prev: asyncio.BufferedProtocol | Up: asyncio | Next: asyncio.capture_call_graph
Rel: asyncio.AbstractEventLoop, asyncio.AbstractServer, asyncio.all_tasks, asyncio.as_completed
Ref: docs.python.org โ asyncio.CancelledError
Tags: #public #python3 #stdlib #asyncio
Import
import asyncio
from asyncio import CancelledError
Signature & Overview
asyncio.CancelledError(...)
The Future or Task was cancelled.
Methods & Attributes
CancelledError.add_note(self, note, /)โ Add a note to the exceptionCancelledError.with_traceback(self, tb, /)โ Set self.traceback to tb and return self.
Usage
import asyncio
async def main():
task = asyncio.create_task(asyncio.sleep(10))
task.cancel()
try:
await task
except asyncio.CancelledError:
assert task.cancelled()
asyncio.run(main())
Gotchas
- Inherits from
BaseException, NOTException(Python 3.8+):except Exception:won't accidentally swallow task cancellation; if you catchCancelledErrorfor cleanup, always re-raise it.
REPL Inspect
from asyncio import CancelledError
help(CancelledError)
Linked from
Related notes
- tuts/python3/stdlib_module/asyncio/BufferedProtocol Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/capture_call_graph Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractEventLoop Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractServer Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/all_tasks Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/as_completed Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/Barrier shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseEventLoop shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseProtocol shared tags: #asyncio, #stdlib