awaitlet


Nameawaitlet JSON
Version 0.0.0 PyPI version JSON
download
home_pageNone
Summaryinvoke asyncio awaitables from non-async functions
upload_time2024-06-12 18:53:36
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseCopyright 2024 Michael Bayer 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
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ========
awaitlet
========

Call Python asyncio awaitables from functions that are not declared
as async.

Synopsis
========

Consider the following asyncio program that sends and receives messages
from an echo server::

    import asyncio

    async def sendrecv(msg):
        reader, writer = await asyncio.open_connection("tcpbin.com", 4242)
        writer.write(f"message number {msg}\n".encode("ascii"))
        await writer.drain()
        data = (await reader.read(1024)).decode("utf-8")
        return data


    async def main():
        messages = await asyncio.gather(
            *[
                sendrecv(msg) for msg in
                ["one", "two", "three", "four", "five"]
            ]
        )
        for msg in messages:
            print(f"Got back echo response: {msg}")

    asyncio.run(main())

What if ``sendrecv`` above wanted to be a function available in existing
code that didn't use ``async``?   With awaitlet we can remove the ``async``
keyword and still have a way of invoking ``async`` awaitables inside
of it::


    import asyncio
    from awaitlet import async_def
    from awaitlet import awaitlet

    def sendrecv_implementation(msg):
        reader, writer = awaitlet(asyncio.open_connection("tcpbin.com", 4242))
        writer.write(f"message number {msg}\n".encode("ascii"))
        awaitlet(writer.drain())
        data = (awaitlet(reader.read(1024))).decode("utf-8")
        return data

    async def sendrecv(msg):
        return await async_def(sendrecv_implementation, msg)

    async def main():
        messages = await asyncio.gather(
            *[
                sendrecv(msg) for msg in
                ["one", "two", "three", "four", "five"]
            ]
        )
        for msg in messages:
            print(f"Got back echo response: {msg}")

    asyncio.run(main())




            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "awaitlet",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "asyncio",
    "author": null,
    "author_email": "Mike Bayer <mike_mp@zzzcomputing.com>",
    "download_url": "https://files.pythonhosted.org/packages/db/87/9c05e8423897ca465c348cf94090de66517b514f9d7bc0b3c64edb02f029/awaitlet-0.0.0.tar.gz",
    "platform": null,
    "description": "========\nawaitlet\n========\n\nCall Python asyncio awaitables from functions that are not declared\nas async.\n\nSynopsis\n========\n\nConsider the following asyncio program that sends and receives messages\nfrom an echo server::\n\n    import asyncio\n\n    async def sendrecv(msg):\n        reader, writer = await asyncio.open_connection(\"tcpbin.com\", 4242)\n        writer.write(f\"message number {msg}\\n\".encode(\"ascii\"))\n        await writer.drain()\n        data = (await reader.read(1024)).decode(\"utf-8\")\n        return data\n\n\n    async def main():\n        messages = await asyncio.gather(\n            *[\n                sendrecv(msg) for msg in\n                [\"one\", \"two\", \"three\", \"four\", \"five\"]\n            ]\n        )\n        for msg in messages:\n            print(f\"Got back echo response: {msg}\")\n\n    asyncio.run(main())\n\nWhat if ``sendrecv`` above wanted to be a function available in existing\ncode that didn't use ``async``?   With awaitlet we can remove the ``async``\nkeyword and still have a way of invoking ``async`` awaitables inside\nof it::\n\n\n    import asyncio\n    from awaitlet import async_def\n    from awaitlet import awaitlet\n\n    def sendrecv_implementation(msg):\n        reader, writer = awaitlet(asyncio.open_connection(\"tcpbin.com\", 4242))\n        writer.write(f\"message number {msg}\\n\".encode(\"ascii\"))\n        awaitlet(writer.drain())\n        data = (awaitlet(reader.read(1024))).decode(\"utf-8\")\n        return data\n\n    async def sendrecv(msg):\n        return await async_def(sendrecv_implementation, msg)\n\n    async def main():\n        messages = await asyncio.gather(\n            *[\n                sendrecv(msg) for msg in\n                [\"one\", \"two\", \"three\", \"four\", \"five\"]\n            ]\n        )\n        for msg in messages:\n            print(f\"Got back echo response: {msg}\")\n\n    asyncio.run(main())\n\n\n\n",
    "bugtrack_url": null,
    "license": "Copyright 2024 Michael Bayer  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": "invoke asyncio awaitables from non-async functions",
    "version": "0.0.0",
    "project_urls": {
        "Homepage": "https://github.com/sqlalchemy/awaitlet"
    },
    "split_keywords": [
        "asyncio"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "db879c05e8423897ca465c348cf94090de66517b514f9d7bc0b3c64edb02f029",
                "md5": "6b0802df4c03c3a4736c9b775adb006c",
                "sha256": "e8e6ba4d3535ef82fef6de025d2c0ae82cef48a675875f743eb89d34110142be"
            },
            "downloads": -1,
            "filename": "awaitlet-0.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "6b0802df4c03c3a4736c9b775adb006c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 12255,
            "upload_time": "2024-06-12T18:53:36",
            "upload_time_iso_8601": "2024-06-12T18:53:36.845915Z",
            "url": "https://files.pythonhosted.org/packages/db/87/9c05e8423897ca465c348cf94090de66517b514f9d7bc0b3c64edb02f029/awaitlet-0.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-12 18:53:36",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sqlalchemy",
    "github_project": "awaitlet",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "awaitlet"
}
        
Elapsed time: 0.73952s