Name | revolving-asyncio JSON |
Version |
0.1.1
JSON |
| download |
home_page | |
Summary | easy to covert between sync and async |
upload_time | 2023-09-07 14:37:27 |
maintainer | |
docs_url | None |
author | |
requires_python | >=3.6 |
license | MIT |
keywords |
sync
async
asyncio
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# revolving_asyncio
同步异步互转工具
1. 本项目依赖于[nest_asyncio](https://github.com/erdewit/nest_asyncio) 在嵌套场景下,需要对`asyncio`打补丁
2. 本项目提取于[ksrpc](https://github.com/wukan1986/ksrpc) 中的同步异步任意转换功能
## Installation安装
```commandline
pip install revolving_asyncio -U
```
## Usage使用
See more [examples](examples)
```python
import asyncio
import time
from revolving_asyncio import to_async, to_sync
if __name__ == '__main__':
@to_async
def do_sync_work(name: str):
time.sleep(5)
print(f"sync function, to async by decorator, {name}")
async def do_async_work(name: str):
await asyncio.sleep(5)
print(f"async function, to sync by wrapper, {name}")
async def async_main():
# Method 1
await do_sync_work(name="Method 1")
def sync_main():
# Method 2
to_sync(do_async_work)(name="Method 2")
asyncio.run(async_main())
sync_main()
```
## Exception 异常
如果直接使用使用`to_async`和`to_sync`报`RuntimeError: This event loop is already running`等一类的异常,可以在之前添加以下代码试试
```python
import revolving_asyncio
revolving_asyncio.apply()
```
Raw data
{
"_id": null,
"home_page": "",
"name": "revolving-asyncio",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "sync,async,asyncio",
"author": "",
"author_email": "wukan <wu-kan@163.com>",
"download_url": "https://files.pythonhosted.org/packages/32/2c/f0d80b59d487a8a3cbb69efaa0a09ad0a2ea5212792510f638b2c139f0ac/revolving_asyncio-0.1.1.tar.gz",
"platform": null,
"description": "# revolving_asyncio\r\n\u540c\u6b65\u5f02\u6b65\u4e92\u8f6c\u5de5\u5177\r\n\r\n1. \u672c\u9879\u76ee\u4f9d\u8d56\u4e8e[nest_asyncio](https://github.com/erdewit/nest_asyncio) \u5728\u5d4c\u5957\u573a\u666f\u4e0b\uff0c\u9700\u8981\u5bf9`asyncio`\u6253\u8865\u4e01\r\n2. \u672c\u9879\u76ee\u63d0\u53d6\u4e8e[ksrpc](https://github.com/wukan1986/ksrpc) \u4e2d\u7684\u540c\u6b65\u5f02\u6b65\u4efb\u610f\u8f6c\u6362\u529f\u80fd\r\n\r\n## Installation\u5b89\u88c5\r\n```commandline\r\npip install revolving_asyncio -U\r\n```\r\n\r\n## Usage\u4f7f\u7528\r\nSee more [examples](examples)\r\n```python\r\nimport asyncio\r\nimport time\r\n\r\nfrom revolving_asyncio import to_async, to_sync\r\n\r\nif __name__ == '__main__':\r\n @to_async\r\n def do_sync_work(name: str):\r\n time.sleep(5)\r\n print(f\"sync function, to async by decorator, {name}\")\r\n\r\n\r\n async def do_async_work(name: str):\r\n await asyncio.sleep(5)\r\n print(f\"async function, to sync by wrapper, {name}\")\r\n\r\n\r\n async def async_main():\r\n # Method 1\r\n await do_sync_work(name=\"Method 1\")\r\n\r\n\r\n def sync_main():\r\n # Method 2\r\n to_sync(do_async_work)(name=\"Method 2\")\r\n\r\n\r\n asyncio.run(async_main())\r\n sync_main()\r\n\r\n```\r\n\r\n## Exception \u5f02\u5e38\r\n\u5982\u679c\u76f4\u63a5\u4f7f\u7528\u4f7f\u7528`to_async`\u548c`to_sync`\u62a5`RuntimeError: This event loop is already running`\u7b49\u4e00\u7c7b\u7684\u5f02\u5e38\uff0c\u53ef\u4ee5\u5728\u4e4b\u524d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u8bd5\u8bd5\r\n```python\r\nimport revolving_asyncio\r\nrevolving_asyncio.apply()\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "easy to covert between sync and async",
"version": "0.1.1",
"project_urls": null,
"split_keywords": [
"sync",
"async",
"asyncio"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "3232c7c16359279e2b44e624a28de527f7369b77567dca900a4fb1b2a8b73727",
"md5": "fabdba9cdd23e3887ae31cfc22b332ed",
"sha256": "5d96e158ca84a5c27590fb795b6dbac57a6deb0258ad01bfebfdb3a01bec500b"
},
"downloads": -1,
"filename": "revolving_asyncio-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "fabdba9cdd23e3887ae31cfc22b332ed",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 3754,
"upload_time": "2023-09-07T14:37:24",
"upload_time_iso_8601": "2023-09-07T14:37:24.014605Z",
"url": "https://files.pythonhosted.org/packages/32/32/c7c16359279e2b44e624a28de527f7369b77567dca900a4fb1b2a8b73727/revolving_asyncio-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "322cf0d80b59d487a8a3cbb69efaa0a09ad0a2ea5212792510f638b2c139f0ac",
"md5": "3b38e11a0b2c18e42b2913362d7e8860",
"sha256": "d01e5b011c2782922e0261816efb8065484484c22d33d6dcf6ff9bd1eacf8bcd"
},
"downloads": -1,
"filename": "revolving_asyncio-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "3b38e11a0b2c18e42b2913362d7e8860",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 3370,
"upload_time": "2023-09-07T14:37:27",
"upload_time_iso_8601": "2023-09-07T14:37:27.841335Z",
"url": "https://files.pythonhosted.org/packages/32/2c/f0d80b59d487a8a3cbb69efaa0a09ad0a2ea5212792510f638b2c139f0ac/revolving_asyncio-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-09-07 14:37:27",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "revolving-asyncio"
}