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()) behaves the same as task = asyncio.create_task(my_async_func()) except that:

  • the task still has to be launched in the background with an existing task group tg, using tg.start_soon(task.wait),
  • and/or the task can be awaited with result = await task.wait().
from anyioutils import CancelledError, 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 = create_task(foo())
        assert await task.wait() == 1

    try:
        async with create_task_group() as tg:
            task = create_task(bar())
            tg.start_soon(task.wait)
            await sleep(0.1)
            task.cancel()
    except ExceptionGroup 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.3.0.tar.gz (5.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.3.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for anyioutils-0.3.0.tar.gz
Algorithm Hash digest
SHA256 8301d3da1ea4e96ed3e5ef88577281bad15e3350b630ec82d36ec1f47750f44b
MD5 b2b4a2a8f6be4b5b0e94d363e68af375
BLAKE2b-256 4e6b85d0f5fe5915b23113c371dd92cb7251dcc74e48a94a89a9b2449b6aaed5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: anyioutils-0.3.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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1e672d45c9a2cc5e74f5bb95fca0c33338019fa641d750cd31e4ced5eb7d14be
MD5 59390363dbdb6441e0b061746b146177
BLAKE2b-256 59e9d3de95df9e84855ed2b412ce8bd94a94fcbeb9d34968d3e1ee05f62639be

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