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 provides 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 that limits the number of concurrently 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 to the event loop. It is a more elegant approach to do-nothing tasks, giving other tasks a chance 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 to 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 callable at a regular interval.

from asyncio_extensions import heartbeat

async def ping():
    pass

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

    await some_long_running_process()

asyncify

The asyncify function ensures a callable can be awaited. If the callable is already a coroutine function, it is returned as-is. Otherwise, it is wrapped so that calls run in a separate thread.

from asyncio_extensions import asyncify

def blocking_read(path: str) -> str:
    with open(path) as f:
        return f.read()

async def main():
    content = await asyncify(blocking_read)("data.txt")

It can also be used as a decorator:

from asyncio_extensions import asyncify

@asyncify
def blocking_read(path: str) -> str:
    with open(path) as f:
        return f.read()

async def main():
    content = await blocking_read("data.txt")

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.4.tar.gz (9.1 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.4-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: asyncio_extensions-0.0.4.tar.gz
  • Upload date:
  • Size: 9.1 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.4.tar.gz
Algorithm Hash digest
SHA256 bfc1be3f939e0d897e8fee585ef2bd5226f1327fe5500cb49ab943f35fdf3247
MD5 0689b1a6ffa63d831c9b6f2d8f0cb0d5
BLAKE2b-256 4157aeff36eef91781a332d510d70e6c397de2b605430064bbd149f0af43284e

See more details on using hashes here.

Provenance

The following attestation bundles were made for asyncio_extensions-0.0.4.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.4-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncio_extensions-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 69931da2224e816de0a75d2583368a946d4871e5b5d99528fcd2c34856e424d9
MD5 ee3870598835c4fc3b5039695c980898
BLAKE2b-256 42bcb263534b9336c36180858d235977fe9b74449e2f71d2dcca607457ac8816

See more details on using hashes here.

Provenance

The following attestation bundles were made for asyncio_extensions-0.0.4-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