Metadata-Version: 2.3
Name: anyioutils
Version: 0.4.9
Summary: Utility classes and functions for AnyIO
Project-URL: Repository, https://github.com/davidbrochart/anyioutils.git
Author-email: David Brochart <david.brochart@gmail.com>
License: Copyright (c) 2024 David Brochart
        
        Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Keywords: asyncio,trio
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python
Requires-Python: >=3.9
Requires-Dist: anyio
Provides-Extra: test
Requires-Dist: coverage; extra == 'test'
Requires-Dist: exceptiongroup; (python_version < '3.11') and extra == 'test'
Requires-Dist: mypy; extra == 'test'
Requires-Dist: pytest; extra == 'test'
Requires-Dist: ruff; extra == 'test'
Requires-Dist: trio; extra == 'test'
Description-Content-Type: text/markdown

# 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()`.

```py
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`.

```py
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.

```py
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)
```
