首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >python 协程新版

python 协程新版

作者头像
Dean0731
发布于 2020-10-10 17:14:20
发布于 2020-10-10 17:14:20
5860
举报
文章被收录于专栏:blog-技术博客blog-技术博客
代码语言:javascript
复制
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])
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-10-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档