Skip to main content

easy to covert between sync and async

Project description

revolving_asyncio

同步异步互转工具

  1. 本项目依赖于nest_asyncio 在嵌套场景下,需要对asyncio打补丁
  2. 本项目提取于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_asyncto_syncRuntimeError: This event loop is already running等一类的异常,可以在之前添加以下代码试试

import revolving_asyncio
revolving_asyncio.apply()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

revolving_asyncio-0.1.1.tar.gz (3.4 kB view hashes)

Uploaded Source

Built Distribution

revolving_asyncio-0.1.1-py3-none-any.whl (3.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page