A utility for cancelling asyncio task groups.
Project description
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asyncio_cancel_scope-0.3.0.tar.gz.
File metadata
- Download URL: asyncio_cancel_scope-0.3.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d64b2ed9d21645c1c7736ef398eb8d8e79c7d9d171b7ace943cefbf2e9f8a8e
|
|
| MD5 |
9892dda765522a2d2a94aa485df0b0a8
|
|
| BLAKE2b-256 |
464e1b3a14cbad1ecdc66784a3e48bd31b10e103aace5a58183ba4993e82158e
|
File details
Details for the file asyncio_cancel_scope-0.3.0-py3-none-any.whl.
File metadata
- Download URL: asyncio_cancel_scope-0.3.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.21
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c03c31c412baaa7128c1de307a15d1185e0cb55d0c734e1aeb25d34db2ec374
|
|
| MD5 |
cef185f0ba860f23119510f94d76000e
|
|
| BLAKE2b-256 |
f025338c76613eedb41cf972951581d27159018c15281a77ed5a530ff3a989ce
|