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
File details
Details for the file revolving_asyncio-0.1.1.tar.gz
.
File metadata
- Download URL: revolving_asyncio-0.1.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d01e5b011c2782922e0261816efb8065484484c22d33d6dcf6ff9bd1eacf8bcd |
|
MD5 | 3b38e11a0b2c18e42b2913362d7e8860 |
|
BLAKE2b-256 | 322cf0d80b59d487a8a3cbb69efaa0a09ad0a2ea5212792510f638b2c139f0ac |
File details
Details for the file revolving_asyncio-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: revolving_asyncio-0.1.1-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d96e158ca84a5c27590fb795b6dbac57a6deb0258ad01bfebfdb3a01bec500b |
|
MD5 | fabdba9cdd23e3887ae31cfc22b332ed |
|
BLAKE2b-256 | 3232c7c16359279e2b44e624a28de527f7369b77567dca900a4fb1b2a8b73727 |