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
Close
Hashes for revolving_asyncio-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d96e158ca84a5c27590fb795b6dbac57a6deb0258ad01bfebfdb3a01bec500b |
|
MD5 | fabdba9cdd23e3887ae31cfc22b332ed |
|
BLAKE2b-256 | 3232c7c16359279e2b44e624a28de527f7369b77567dca900a4fb1b2a8b73727 |