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中直接使用

浙公网安备 33010602011771号