[Python]异步run_until_complete和run_forever

import asyncio
from asyncio import Future


async def f1():
    print(1)
    await asyncio.sleep(3)
    print(2)
    return "f1"


def callback(f: Future):
    f.get_loop().stop()


loop = asyncio.get_event_loop()
task = loop.create_task(f1())
# 任务结束后回调终止
task.add_done_callback(callback)
# 循环
loop.run_forever()

posted @ 2023-03-31 09:09  LeoShi2020  阅读(55)  评论(0编辑  收藏  举报