Skip to main content

Utility classes and functions for AnyIO

Project description

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)

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.4.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

anyioutils-0.4.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: anyioutils-0.4.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for anyioutils-0.4.0.tar.gz
Algorithm Hash digest
SHA256 bc4155b5d6284b91bc834af1f75a91f5f445da54711966df5e865921333e85d0
MD5 df7099d6d77eb4a9112300e1e8b70fec
BLAKE2b-256 2c58a9b1382afb5ffc120f280c62bceebb2a4bd5a78995c3ee20204055b004f0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: anyioutils-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for anyioutils-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6742e57fa230dec78c950421c0e7e830da25b476fa375c8506e6ebc5bdb7c770
MD5 8f08c1e54705f59485caedbef5d1f88b
BLAKE2b-256 07a58d5f25358e7bf929ef515e89107ea14d3644dc42262757f2e5d5d4cb4e84

See more details on using hashes here.

Supported by

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