https://blog.csdn.net/rhx_qiuzhi/article/details/124332114
https://docs.python.org/zh-cn/3/library/asyncio-task.html
async def washing1():
await asyncio.sleep(3)
print('小朋友的衣服洗完了')
async def washing2():
await asyncio.sleep(2)
print('爷爷奶奶的衣服洗完了')
async def washing3():
await asyncio.sleep(5)
print('爸爸妈妈的衣服洗完了')
#异步启动主函数
async def main():
#调用方法一
# tasks=[asyncio.create_task(washing1()),
# asyncio.create_task(washing2()),
# asyncio.create_task(washing3())]
# done,pending=await asyncio.wait(tasks)
# print('done', done,'pending', pending)
#调用方法二
tasks = [asyncio.ensure_future(washing1()),
asyncio.ensure_future(washing2()),
asyncio.ensure_future(washing3())]
done,pending =await asyncio.wait(tasks)
print('done', done,'pending', pending)
# 运行方法三 gather #除了多任务外,还可以对任务进行分组。优先使用gather
# tasks1 = [washing1(), washing2(), washing3()]
# group1 = asyncio.gather(*tasks1)
# done =await asyncio.gather(group1)
# print('done',done)
if __name__=="__main__":
startTime = time.time()
loop = asyncio.get_event_loop() # 创建一个事件循环
try:
loop.run_until_complete(main()) #完成事件循环,直到最后一个任务结束
finally:
loop.close() #结束事件循环
print("所有IO任务总耗时: ",time.time()-startTime)
https://zhuanlan.zhihu.com/p/76536655
https://blog.csdn.net/rhx_qiuzhi/article/details/124332114