python异步之async_await

import asyncio
async def f1():
print(1)
await asyncio.sleep(2)
print(2)

async def f2():
print(3)
await asyncio.sleep(2)
print(4)

tasks=[
asyncio.ensure_future(f1()),
asyncio.ensure_future(f2())
]
loop=asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
#await只能在async内部使用,但是可以在ipython中直接使用
posted @ 2023-03-10 17:41  听见时间  阅读(88)  评论(0)    收藏  举报