Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

asyncio.wait和asyncio.gather的区别?

刚学PYTHON的协程,我想请教下,这两段代码执行起来有什么区别呢?

tasks = [asyncio.ensure_future(task(i)) for i in range(0,300)]
loop.run_until_complete(asyncio.gather(*tasks))

tasks = [task(i) for i in range(0,300)]
loop.run_until_complete(asyncio.wait(tasks))



与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

看文档就知道了,这两段代码的效果相同。但是 wait 和 gather 的返回值不一样,wait 也可以在第一个 future 完全或者出错时就返回。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...