python 协程新版
import asyncio
async def coro(i):
print("core start",i)
ret = await get(i) # 此处为耗时的io等操作
print("core end",i)
return ret
async def get(i):
await asyncio.sleep(5)
return i
loop = asyncio.get_event_loop()
a = [asyncio.ensure_future(coro(i)) for i in range(10)]
loop.run_until_complete(asyncio.wait(a))
loop.close()
print([i.result() for i in a])
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,如有问题, 可评论咨询.