universalasync


Nameuniversalasync JSON
Version 0.3.1.2 PyPI version JSON
download
home_pagehttps://github.com/bitcart/universalasync
SummaryA library to help automate the creation of universal python libraries
upload_time2024-03-15 21:22:46
maintainer
docs_urlNone
authorMrNaif2018
requires_python>=3.8
licenseMIT
keywords async await bitcart universal sync asyncio asynctosync synctoasync
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # universalasync

[![CircleCI](https://circleci.com/gh/bitcart/universalasync.svg?style=svg)](https://circleci.com/gh/bitcart/universalasync)
[![Codecov](https://img.shields.io/codecov/c/github/bitcart/universalasync?style=flat-square)](https://codecov.io/gh/bitcart/universalasync)
[![PyPI version](https://img.shields.io/pypi/v/universalasync.svg?style=flat-square)](https://pypi.python.org/pypi/universalasync/)
[![Read the Docs](https://img.shields.io/readthedocs/universalasync?style=flat-square)](https://universalasync.bitcart.ai)

A library to help automate the creation of universal python libraries

## Overview

Have you ever been frustrated that you need to maintain both sync and async versions of your library, even thought their code differs by just async and await?
You might have came up to rewriting your code before release or other unreliable solutions.

This library helps you to focus only on the main async implementation of your library: sync one will be created automatically

Via decorating all your public methods, the wrapped functions automatically detect different conditions and run the functions accordingly.

If user uses your library in async context, minimal overhead is added, it just returns the coroutine right away.

Otherwise the library calls the coroutine via various loop methods, like as if you did it manually.

There should be no issues at all, the only limitation is that signals and async subprocesses are supported only when running in the main thread.

Also note that when run from a different os thread, the library will create a new event loop there and run coroutines.

This means that you might need to adapt your code a bit in case you use some resources bound to a certain event loop (like `aiohttp.ClientSession`).

You can see an example of how this could be solved [here](https://github.com/bitcart/bitcart-sdk/blob/4a425f80f62a0c90f8c5fa19ccb7e578590dcead/bitcart/providers/jsonrpcrequests.py#L51-L58)

For API reference see [the docs](https://universalasync.bitcart.ai)

## Installation

`pip install universalasync`

## Example of usage

```python
# wrap needed methods one by one
class Client:
    @async_to_sync_wraps
    async def help():
        ...

    @async_to_sync_wraps
    @property
    async def async_property():
        ...

# or wrap whole classes
@wrap
class Client:
    async def help(self):
        ...

    @property
    async def async_property():
        ...

client = Client()

def sync_call():
    client.help()
    client.async_property

async def async_call():
    await client.help()
    await client.async_property

# works in all cases
sync_call()
asyncio.run(async_call())
threading.Thread(target=sync_call).start()
threading.Thread(target=asyncio.run, args=(async_call(),)).start()
```

## Copyright and License

Copyright (C) 2021 MrNaif2018

Universal async-sync wrapper initially based on [this implementation](https://github.com/pyrogram/pyrogram/blob/master/pyrogram/sync.py)

Licensed under the [MIT license](LICENSE)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/bitcart/universalasync",
    "name": "universalasync",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "async,await,bitcart,universal,sync,asyncio,asynctosync,synctoasync",
    "author": "MrNaif2018",
    "author_email": "chuff184@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/75/31/54edc90fa94904046297eeef30878d271e21c1c1dcad18b75bbcbb58ade6/universalasync-0.3.1.2.tar.gz",
    "platform": null,
    "description": "# universalasync\n\n[![CircleCI](https://circleci.com/gh/bitcart/universalasync.svg?style=svg)](https://circleci.com/gh/bitcart/universalasync)\n[![Codecov](https://img.shields.io/codecov/c/github/bitcart/universalasync?style=flat-square)](https://codecov.io/gh/bitcart/universalasync)\n[![PyPI version](https://img.shields.io/pypi/v/universalasync.svg?style=flat-square)](https://pypi.python.org/pypi/universalasync/)\n[![Read the Docs](https://img.shields.io/readthedocs/universalasync?style=flat-square)](https://universalasync.bitcart.ai)\n\nA library to help automate the creation of universal python libraries\n\n## Overview\n\nHave you ever been frustrated that you need to maintain both sync and async versions of your library, even thought their code differs by just async and await?\nYou might have came up to rewriting your code before release or other unreliable solutions.\n\nThis library helps you to focus only on the main async implementation of your library: sync one will be created automatically\n\nVia decorating all your public methods, the wrapped functions automatically detect different conditions and run the functions accordingly.\n\nIf user uses your library in async context, minimal overhead is added, it just returns the coroutine right away.\n\nOtherwise the library calls the coroutine via various loop methods, like as if you did it manually.\n\nThere should be no issues at all, the only limitation is that signals and async subprocesses are supported only when running in the main thread.\n\nAlso note that when run from a different os thread, the library will create a new event loop there and run coroutines.\n\nThis means that you might need to adapt your code a bit in case you use some resources bound to a certain event loop (like `aiohttp.ClientSession`).\n\nYou can see an example of how this could be solved [here](https://github.com/bitcart/bitcart-sdk/blob/4a425f80f62a0c90f8c5fa19ccb7e578590dcead/bitcart/providers/jsonrpcrequests.py#L51-L58)\n\nFor API reference see [the docs](https://universalasync.bitcart.ai)\n\n## Installation\n\n`pip install universalasync`\n\n## Example of usage\n\n```python\n# wrap needed methods one by one\nclass Client:\n    @async_to_sync_wraps\n    async def help():\n        ...\n\n    @async_to_sync_wraps\n    @property\n    async def async_property():\n        ...\n\n# or wrap whole classes\n@wrap\nclass Client:\n    async def help(self):\n        ...\n\n    @property\n    async def async_property():\n        ...\n\nclient = Client()\n\ndef sync_call():\n    client.help()\n    client.async_property\n\nasync def async_call():\n    await client.help()\n    await client.async_property\n\n# works in all cases\nsync_call()\nasyncio.run(async_call())\nthreading.Thread(target=sync_call).start()\nthreading.Thread(target=asyncio.run, args=(async_call(),)).start()\n```\n\n## Copyright and License\n\nCopyright (C) 2021 MrNaif2018\n\nUniversal async-sync wrapper initially based on [this implementation](https://github.com/pyrogram/pyrogram/blob/master/pyrogram/sync.py)\n\nLicensed under the [MIT license](LICENSE)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A library to help automate the creation of universal python libraries",
    "version": "0.3.1.2",
    "project_urls": {
        "Homepage": "https://github.com/bitcart/universalasync"
    },
    "split_keywords": [
        "async",
        "await",
        "bitcart",
        "universal",
        "sync",
        "asyncio",
        "asynctosync",
        "synctoasync"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "534a4d25b3debdc02cf5b2dfc4d7a8698309c5ffd72ea5b77e9e1723771a2957",
                "md5": "ae764ae8c7e83b77361860872f90de95",
                "sha256": "5324996827402e1b509ca200b324615cb0b50f1f82f11afaf538533355a34d45"
            },
            "downloads": -1,
            "filename": "universalasync-0.3.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "ae764ae8c7e83b77361860872f90de95",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6015,
            "upload_time": "2024-03-15T21:22:44",
            "upload_time_iso_8601": "2024-03-15T21:22:44.919468Z",
            "url": "https://files.pythonhosted.org/packages/53/4a/4d25b3debdc02cf5b2dfc4d7a8698309c5ffd72ea5b77e9e1723771a2957/universalasync-0.3.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "753154edc90fa94904046297eeef30878d271e21c1c1dcad18b75bbcbb58ade6",
                "md5": "22aa76746145d2ba34c3238b115e73f8",
                "sha256": "72271f3b35bc3bd6e72cab125d2cf19f178386c66d3c47e39bc2ddc4de974f6c"
            },
            "downloads": -1,
            "filename": "universalasync-0.3.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "22aa76746145d2ba34c3238b115e73f8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 9188,
            "upload_time": "2024-03-15T21:22:46",
            "upload_time_iso_8601": "2024-03-15T21:22:46.230966Z",
            "url": "https://files.pythonhosted.org/packages/75/31/54edc90fa94904046297eeef30878d271e21c1c1dcad18b75bbcbb58ade6/universalasync-0.3.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-15 21:22:46",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "bitcart",
    "github_project": "universalasync",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "circle": true,
    "lcname": "universalasync"
}
        
Elapsed time: 0.46333s