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()
Metadata
Release files for revolving-asyncio 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| revolving_asyncio-0.1.2.tar.gz | 4.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| revolving_asyncio-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.7 kB
Release files / revolving_asyncio-0.1.2.tar.gz
| Download URL | revolving_asyncio-0.1.2.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3f6fd8bbc47a30ee862f8d8717c758c4045e708c266ff9d812257e81bd781203
|
|
BLAKE2b-256 checksum How to use checksums |
725f3f10e4a28f6b23e3d7caa00070dfdd4467b946e16a495998c2450eaf40b5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.23
|
Release files / revolving_asyncio-0.1.2-py3-none-any.whl
| Download URL | revolving_asyncio-0.1.2-py3-none-any.whl |
|---|---|
| Size | 3.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0351b9e3eea3e97e3830477c3d6d3961c4ed210d0de9cd59fa2675f5ffc94607
|
|
BLAKE2b-256 checksum How to use checksums |
61fdfce42c63ff370ec1a2947948c93393ea1aaf76c88345e5716bbb344f43e9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.23
|