asyncio 协程快速创建三步骤:
1、编写协程函数。
2、创建协程对象。
3、加入事件循环。
import asyncio
async def do_somethings():
# 1、定义协程函数
print(1)
await asyncio.sleep(0.5)
async def do_another():
print(2)
await asyncio.sleep(0.2)
async def do_others():
print(3)
async def main():
# 2、创建协程对象。
await do_somethings()
await do_another()
await do_others()
if __name__ == '__main__':
# 3、加入事件循环。
asyncio.run(main())