Skip to main content

Utility classes and functions for AnyIO

Project description

Build Status Code Coverage

anyioutils

Utility classes and functions for AnyIO.

Task

task = anyioutils.create_task(my_async_func(), task_group) behaves the same as task = asyncio.create_task(my_async_func()) except that an existing task_group has to be passed for the task to be launched in the background.

You can also use task = anyioutils.Task(my_async_func()) and then launch the task with task_group.start_soon(task.wait), and/or await it with result = await task.wait().

from anyioutils import CancelledError, Task, create_task
from anyio import create_task_group, run, sleep

async def foo():
    return 1

async def bar():
    await sleep(float("inf"))

async def main():
    async with create_task_group() as tg:
        task = Task(foo())
        assert await task.wait() == 1

    try:
        async with create_task_group() as tg:
            task = create_task(bar(), tg)
            await sleep(0.1)
            task.cancel()
    except BaseExceptionGroup as exc_group:
        assert len(exc_group.exceptions) == 1
        assert type(exc_group.exceptions[0]) == CancelledError

run(main)

Future

anyioutils.Future behaves the same as asyncio.Future except that:

  • you cannot directly await an anyioutils.Future object, but through its .wait() method (unlike an asyncio.Future, but like an asyncio.Event),
  • cancelling an anyioutils.Future doesn't raise an anyio.get_cancelled_exc_class(), but an anyioutils.CancelledError.
from anyioutils import CancelledError, Future
from anyio import create_task_group, run

async def set_result(future):
    future.set_result("done")

async def cancel(future):
    future.cancel()

async def main():
    async with create_task_group() as tg:
        future0 = Future()
        tg.start_soon(set_result, future0)
        assert await future0.wait() == "done"

        future1 = Future()
        tg.start_soon(cancel, future1)
        try:
            await future1.wait()
        except CancelledError:
            assert future1.cancelled()

run(main)

wait

anyioutils.wait(aws, task_group) behaves the same as asyncio.wait(aws) except that an existing task_group has to be passed.

from anyioutils import ALL_COMPLETED, Task, wait
from anyio import create_task_group, run

async def foo():
    return "foo"

async def main():
    async with create_task_group() as tg:
        tasks = [Task(aw) for aw in (foo(), foo())]
        done, pending = await wait(tasks, tg, return_when=ALL_COMPLETED)
        assert done == set(tasks)
        assert not pending
        for task in done:
            assert task.result() == "foo"

run(main)

Event

anyioutils.Event behaves the same as asyncio.Event.

Queue

anyioutils.Queue behaves the same as asyncio.Queue.

TaskGroup

anyioutils.TaskGroup behaves the same as asyncio.TaskGroup. Furthermore, when it is used, anyioutils.create_task(coro) won't need a task group, as one will be looked up the call stack.

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

anyioutils-0.6.6.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

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

anyioutils-0.6.6-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file anyioutils-0.6.6.tar.gz.

File metadata

  • Download URL: anyioutils-0.6.6.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for anyioutils-0.6.6.tar.gz
Algorithm Hash digest
SHA256 256af866e6dffbe3cfef425562f9b1441fc3bc1d95139d511d2ee2dcedc247ed
MD5 d5efda4e7fb1849ebe9f0e527e9b0897
BLAKE2b-256 50d08c3719848d0d60a2306b03bad2bc9efe7e97f55684c8c0f4d5e95d390e86

See more details on using hashes here.

Provenance

The following attestation bundles were made for anyioutils-0.6.6.tar.gz:

Publisher: publish.yml on davidbrochart/anyioutils

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

File details

Details for the file anyioutils-0.6.6-py3-none-any.whl.

File metadata

  • Download URL: anyioutils-0.6.6-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for anyioutils-0.6.6-py3-none-any.whl
Algorithm Hash digest
SHA256 a27f6d0384c199a36c10178ac2d48fcbcef7cb42af7db8c786445b07ea68684d
MD5 cfb6bfdf0c37a155a9588d0bd5e29f41
BLAKE2b-256 32652810bc428701a49aa6af9417ebd1a8c2510c42366685876039dc76ef560d

See more details on using hashes here.

Provenance

The following attestation bundles were made for anyioutils-0.6.6-py3-none-any.whl:

Publisher: publish.yml on davidbrochart/anyioutils

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