Python循环体内实现异步并发

问题起源:循环体内无法实现异步。

Python使用异步模块Asyncio实现多线程并发,一般方式是:

async def func():
    # code...
    
async def main():
    await(func())

if __name__ =='__main__':
    asyncio.run(main())

但实验过程中有个需求,是让循环体的每次循环都作为一个并发线程产生并发。

这种情况下,每次循环使用await调用异步函数,无法实现需求中的并发需求。

asyncio程序如下:

import time
import asyncio

async def hello():
    await asyncio.sleep(1)
    print('Hello World:%s' % time.time())

async def main():
    start = time.time()
    for i in range(5):
        await(hello())
    print("use time: %f s" % (time.time()-start))

if __name__ =='__main__':
    asyncio.run(main())

程序结果:

Hello World:1608368438.992576
Hello World:1608368439.9939594
Hello World:1608368440.9950461
Hello World:1608368441.9971309
Hello World:1608368443.00034
use time: 5.008629 s

程序运行时间是5秒,意味着并未达到异步的效果。

原因:整个for循环体是一个协程,协程切换时会挂起整个main协程。

解决办法:使用asyncio.gather()

asyncio.gather()需要输入一个任务列表,gather会划分任务,并分组执行,因此可以应对for循环体内的异步。

完善后的代码:

import time
import asyncio

async def hello():
    await asyncio.sleep(1)
    print('Hello World:%s' % time.time())

async def main():
    tasks=[]
    for i in range(5):
        tasks.append(hello())
    await asyncio.gather(*tasks)

if __name__ =='__main__':
    asyncio.run(main())

程序运行结果:

Hello World:1608368546.8756351
Hello World:1608368546.8756351
Hello World:1608368546.8756351
Hello World:1608368546.8756351
Hello World:1608368546.8756351
use time: 1.002837 s

程序运行时间是1秒,说明已经达到异步效果。

参考:

异步编程 101:asyncio中的 for 循环

python中的asyncio使用详解

Python中的asyncio代码详解

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容