Asyncio Cancel Scope
A utility for cancelling asyncio task groups in the absence of one from the standard library.
Installation
pip install asyncio_cancel_scope
Usage
The cancel_scope function allows you to establish a task group that can be cancelled
with the cancel_group function. This is useful for managing multiple tasks that should
be stopped together.
import asyncio
from asyncio_cancel_scope import cancel_scope, cancel_group
async def main():
async with cancel_scope(asyncio.TaskGroup()) as tg:
tg.create_task(asyncio.sleep(1))
tg.create_task(asyncio.sleep(2))
tg.create_task(asyncio.sleep(3))
tg.create_task(asyncio.sleep(4))
cancel_group(tg) # cancels all tasks in the group and exits without an exception
asyncio.run(main())
You can link cancel scopes so that parent task groups are cancelled when inner task groups are.
import asyncio
from asyncio_cancel_scope import cancel_scope, cancel_group
async def outer():
forever = asyncio.Event()
async with cancel_scope(asyncio.TaskGroup()) as outer_tg:
outer_tg.create_task(forever.wait())
outer_tg.create_task(inner(outer_tg))
async def inner(parent_tg):
async with cancel_scope(asyncio.TaskGroup(), parent_tg) as inner_tg:
cancel_group(inner_tg)
asyncio.run(outer())
Alternatives
Without this you'd need to manually cancel each task in the group, which can be cumbersome and error-prone:
import asyncio
async def main():
tasks = []
async with asyncio.TaskGroup() as tg:
tasks.append(tg.create_task(asyncio.sleep(1)))
tasks.append(tg.create_task(asyncio.sleep(2)))
tasks.append(tg.create_task(asyncio.sleep(3)))
tasks.append(tg.create_task(asyncio.sleep(4)))
for task in tasks:
task.cancel() # manually cancel each task
Under the Hood
The cancel_group function adds a task to the task group that immediately raises a
special exception. When this happens the behavior of asyncio.TaskGroup is to cancel
all tasks in the group and propagate the exception. The purpose of cancel_scope is to
supress that special exception so that it does not propagate to the caller of
cancel_scope.
Metadata
Release files for asyncio_cancel_scope 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| asyncio_cancel_scope-0.3.0.tar.gz | 6.6 kB | Without provenance | Details for asyncio_cancel_scope-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| asyncio_cancel_scope-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for asyncio_cancel_scope-0.3.0-py3-none-any.whl |
Total release size: 11.9 kB
Release files / asyncio_cancel_scope-0.3.0.tar.gz
| Download URL | asyncio_cancel_scope-0.3.0.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8d64b2ed9d21645c1c7736ef398eb8d8e79c7d9d171b7ace943cefbf2e9f8a8e
|
|
BLAKE2b-256 checksum How to use checksums |
464e1b3a14cbad1ecdc66784a3e48bd31b10e103aace5a58183ba4993e82158e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.21
|
Release files / asyncio_cancel_scope-0.3.0-py3-none-any.whl
| Download URL | asyncio_cancel_scope-0.3.0-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3c03c31c412baaa7128c1de307a15d1185e0cb55d0c734e1aeb25d34db2ec374
|
|
BLAKE2b-256 checksum How to use checksums |
f025338c76613eedb41cf972951581d27159018c15281a77ed5a530ff3a989ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.7.21
|
Security for asyncio_cancel_scope 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.