Posts python3 使用 awaits 库的 @awaitable 装饰器把同步函数专成异步调用
Post
Cancel

python3 使用 awaits 库的 @awaitable 装饰器把同步函数专成异步调用

安装

1
pip install awaits

使用方法,只需向同步函数添加@awaitable装饰器,就可以把该同步函数专成异步调用,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import time
import asyncio
from awaits.awaitable import awaitable

@awaitable
def test(seconds=1):
    time.sleep(seconds)
    

async def async_test():
    print('start')
    await test(2)
    print('done')

loop = asyncio.get_event_loop()
loop.run_until_complete(async_test())
loop.close()

真诚邀请您走进我的知识小宇宙,关注我个人的公众号,在这里,我将不时为您献上独家原创且极具价值的技术内容分享。每一次推送,都倾注了我对技术领域的独特见解与实战心得,旨在与您共享成长过程中的每一份收获和感悟。您的关注和支持,是我持续提供优质内容的最大动力,让我们在学习的道路上并肩同行,共同进步,一起书写精彩的成长篇章!

AI文字转语音
AI个性头像生成
This post is licensed under CC BY 4.0 by the author.

Trending Tags