Skip to main content

Some (maybe) useful extensions for asyncio

Project description

asyncio-extensions

PyPI - Version PyPI - Python Version codecov pre-commit.ci status


Installation

pip install asyncio-extensions

Usage

TaskGroup

asyncio-extensions provide a cancellable version of AsyncIO's TaskGroup.

import asyncio

from asyncio_extensions import TaskGroup

queue = asyncio.Queue()
async with TaskGroup() as tg:
    for _ in range(10):
        tg.create_task(consume_from_queue(queue))

    await add_to_queue(queue)
    await queue.join()
    tg.cancel()

LimitedTaskGroup

A version of TaskGroup which limits the number of running tasks.

import asyncio

from asyncio_extensions import LimitedTaskGroup

queue = asyncio.Queue()
async with LimitedTaskGroup(3) as tg:
    for _ in range(50):
        tg.create_task(some_expensive_operation(queue))

    await add_to_queue(queue)
    await queue.join()
    tg.cancel()

checkpoint

The checkpoint function yields control do the event loop. It is a more elegant approach to do-nothing tasks since they give a chance for other tasks to run.

from asyncio_extensions import checkpoint

class DummyChannel:
    async def send_message(self, message):
        await checkpoint()

sleep_forever

The sleep_forever function never returns. It simply keeps yielding control do the event loop.

from asyncio_extensions import sleep_forever

class DummyChannel:
    async def receive_message(self):
        await sleep_forever()

heartbeat

The heartbeat function runs a given function at a regular interval.

from asyncio_extensions import heartbeat

interval = 5

async def ping():
    pass

async with TaskGroup() as tg:
    tg.create_task(heartbeat(5, ping))

    await some_long_running_process()

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

asyncio-extensions is distributed under the terms of the MIT license.

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_extensions-0.0.3.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

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

asyncio_extensions-0.0.3-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file asyncio_extensions-0.0.3.tar.gz.

File metadata

  • Download URL: asyncio_extensions-0.0.3.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for asyncio_extensions-0.0.3.tar.gz
Algorithm Hash digest
SHA256 130e1b90a8d9dad5849df623b7dbd3e1d03aa205b27fc8d619b8fc33e74e6777
MD5 791f0a4b756037dd2c41130869dc4ca2
BLAKE2b-256 d970db1181e3d7ebd6bc765342c67cc60f3984fbe0d505667b3571d9cd35714e

See more details on using hashes here.

Provenance

The following attestation bundles were made for asyncio_extensions-0.0.3.tar.gz:

Publisher: release.yml on hartungstenio/asyncio-extensions

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file asyncio_extensions-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncio_extensions-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1e421882445c0164356feb0f7e499b62aa98ffaacc248b9cea69c88012840c33
MD5 a6245479a6becb549278b49e4c0100f9
BLAKE2b-256 e806e097580eb0c2492f1f43af54a82acf103ad9d086a70f6af772de7c036351

See more details on using hashes here.

Provenance

The following attestation bundles were made for asyncio_extensions-0.0.3-py3-none-any.whl:

Publisher: release.yml on hartungstenio/asyncio-extensions

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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