easy to covert between sync and async
Project description
revolving_asyncio
同步异步互转工具
- 本项目部分依赖于nest_asyncio, 在嵌套场景下,可能需要对
asyncio打补丁 - 本项目提取于ksrpc 中的同步异步任意转换功能
Installation安装
pip install revolving_asyncio -U
Usage使用
See more examples
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等一类的异常,
可以尝试添加以下代码
import revolving_asyncio
revolving_asyncio.apply()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file revolving_asyncio-0.1.2.tar.gz.
File metadata
- Download URL: revolving_asyncio-0.1.2.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f6fd8bbc47a30ee862f8d8717c758c4045e708c266ff9d812257e81bd781203
|
|
| MD5 |
58ea5c02120dffa838c732e9efd161d1
|
|
| BLAKE2b-256 |
725f3f10e4a28f6b23e3d7caa00070dfdd4467b946e16a495998c2450eaf40b5
|
File details
Details for the file revolving_asyncio-0.1.2-py3-none-any.whl.
File metadata
- Download URL: revolving_asyncio-0.1.2-py3-none-any.whl
- Upload date:
- Size: 3.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0351b9e3eea3e97e3830477c3d6d3961c4ed210d0de9cd59fa2675f5ffc94607
|
|
| MD5 |
fd2e11046fb800c9d60b06aefac4673f
|
|
| BLAKE2b-256 |
61fdfce42c63ff370ec1a2947948c93393ea1aaf76c88345e5716bbb344f43e9
|