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])
posted @ 2020-10-05 20:06  下雨天,真好  阅读(106)  评论(0编辑  收藏  举报