Skip to main content

A utility for cancelling asyncio task groups.

Project description

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.

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

asyncio_cancel_scope-0.3.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asyncio_cancel_scope-0.3.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file asyncio_cancel_scope-0.3.0.tar.gz.

File metadata

File hashes

Hashes for asyncio_cancel_scope-0.3.0.tar.gz
Algorithm Hash digest
SHA256 8d64b2ed9d21645c1c7736ef398eb8d8e79c7d9d171b7ace943cefbf2e9f8a8e
MD5 9892dda765522a2d2a94aa485df0b0a8
BLAKE2b-256 464e1b3a14cbad1ecdc66784a3e48bd31b10e103aace5a58183ba4993e82158e

See more details on using hashes here.

File details

Details for the file asyncio_cancel_scope-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncio_cancel_scope-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3c03c31c412baaa7128c1de307a15d1185e0cb55d0c734e1aeb25d34db2ec374
MD5 cef185f0ba860f23119510f94d76000e
BLAKE2b-256 f025338c76613eedb41cf972951581d27159018c15281a77ed5a530ff3a989ce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page