python异步编程asyncio实现协程

import asyncio
@asyncio.coroutine
def f1():
print(1)
yield from asyncio.sleep(2)
print(2)

@asyncio.coroutine
def f2():
print(3)
yield from 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))
posted @ 2023-03-09 18:20  听见时间  阅读(30)  评论(0)    收藏  举报