anyioutils


Nameanyioutils JSON
Version 0.4.7 PyPI version JSON
download
home_pageNone
SummaryUtility classes and functions for AnyIO
upload_time2024-11-08 08:31:08
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseCopyright (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
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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)
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "anyioutils",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "asyncio, trio",
    "author": null,
    "author_email": "David Brochart <david.brochart@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/71/14/deddd484fc3af9378254e37af315cc689013ee285e6d9eb694ed1fbec04e/anyioutils-0.4.7.tar.gz",
    "platform": null,
    "description": "# anyioutils\n\nUtility classes and functions for AnyIO.\n\n## Task\n\n`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.\n\nYou 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()`.\n\n```py\nfrom anyioutils import CancelledError, Task, create_task\nfrom anyio import create_task_group, run, sleep\n\nasync def foo():\n    return 1\n\nasync def bar():\n    await sleep(float(\"inf\"))\n\nasync def main():\n    async with create_task_group() as tg:\n        task = Task(foo())\n        assert await task.wait() == 1\n\n    try:\n        async with create_task_group() as tg:\n            task = create_task(bar(), tg)\n            await sleep(0.1)\n            task.cancel()\n    except BaseExceptionGroup as exc_group:\n        assert len(exc_group.exceptions) == 1\n        assert type(exc_group.exceptions[0]) == CancelledError\n\nrun(main)\n```\n\n## Future\n\n`anyioutils.Future` behaves the same as `asyncio.Future` except that:\n- you cannot directly await an `anyioutils.Future` object, but through its `.wait()` method (unlike an `asyncio.Future`, but like an `asyncio.Event`),\n- cancelling an `anyioutils.Future` doesn't raise an `anyio.get_cancelled_exc_class()`, but an `anyioutils.CancelledError`.\n\n```py\nfrom anyioutils import CancelledError, Future\nfrom anyio import create_task_group, run\n\nasync def set_result(future):\n    future.set_result(\"done\")\n\nasync def cancel(future):\n    future.cancel()\n\nasync def main():\n    async with create_task_group() as tg:\n        future0 = Future()\n        tg.start_soon(set_result, future0)\n        assert await future0.wait() == \"done\"\n\n        future1 = Future()\n        tg.start_soon(cancel, future1)\n        try:\n            await future1.wait()\n        except CancelledError:\n            assert future1.cancelled()\n\nrun(main)\n```\n",
    "bugtrack_url": null,
    "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.",
    "summary": "Utility classes and functions for AnyIO",
    "version": "0.4.7",
    "project_urls": {
        "Repository": "https://github.com/davidbrochart/anyioutils.git"
    },
    "split_keywords": [
        "asyncio",
        " trio"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5b822e05d788e67dd6ce848c8f5ec90f36001bcefe01ead5e63001b238952a79",
                "md5": "9cdf53ded37053dd00b4c5be930a756f",
                "sha256": "2f9514c5f727f4c9eeb5267dc2268bab728e33803efdfe01069653dce217d701"
            },
            "downloads": -1,
            "filename": "anyioutils-0.4.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9cdf53ded37053dd00b4c5be930a756f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 5932,
            "upload_time": "2024-11-08T08:31:05",
            "upload_time_iso_8601": "2024-11-08T08:31:05.727638Z",
            "url": "https://files.pythonhosted.org/packages/5b/82/2e05d788e67dd6ce848c8f5ec90f36001bcefe01ead5e63001b238952a79/anyioutils-0.4.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7114deddd484fc3af9378254e37af315cc689013ee285e6d9eb694ed1fbec04e",
                "md5": "c5e2779cf5935566da09dced0234e74b",
                "sha256": "aeb6806f69b13a91fdede02b61a0194fd637f0aadf23e813b23a448973a17670"
            },
            "downloads": -1,
            "filename": "anyioutils-0.4.7.tar.gz",
            "has_sig": false,
            "md5_digest": "c5e2779cf5935566da09dced0234e74b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 5410,
            "upload_time": "2024-11-08T08:31:08",
            "upload_time_iso_8601": "2024-11-08T08:31:08.685497Z",
            "url": "https://files.pythonhosted.org/packages/71/14/deddd484fc3af9378254e37af315cc689013ee285e6d9eb694ed1fbec04e/anyioutils-0.4.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-08 08:31:08",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "davidbrochart",
    "github_project": "anyioutils",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "anyioutils"
}
        
Elapsed time: 2.57188s