本文转载自简书:https://www.jianshu.com/p/c321eb22cffd
用户:简单书写_,
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秒,说明已经达到异步效果。