Skip to main content

Asyncio Cancel Scope

PyPI - Version PyPI - Python Version License: MIT

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)

Source distribution for asyncio_cancel_scope 0.3.0
File Size Uploaded
asyncio_cancel_scope-0.3.0.tar.gz 6.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for asyncio_cancel_scope 0.3.0
File Interpreter ABI Platform
asyncio_cancel_scope-0.3.0-py3-none-any.whl Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page