tuoheng_algN/vodsdk/test/协程/asnyc回调.py

28 lines
822 B
Python

import asyncio
import time
async def _task():
print(f"{time.strftime('%H:%M:%S')} task 开始 ")
time.sleep(2)
print(f"{time.strftime('%H:%M:%S')} task 结束")
return "运行结束"
def callback(task):
print(f"{time.strftime('%H:%M:%S')} 回调函数开始运行")
print(f"状态:{task.result()}")
coroutine = _task()
print(f"{time.strftime('%H:%M:%S')} 产生协程对象 {coroutine},函数并未被调用")
task = asyncio.ensure_future(coroutine) # 返回task对象
task.add_done_callback(callback) # 为task增加一个回调任务
loop = asyncio.get_event_loop()
print(f"{time.strftime('%H:%M:%S')} 开始调用协程任务")
start = time.time()
loop.run_until_complete(task)
end = time.time()
print(f"{time.strftime('%H:%M:%S')} 结束调用协程任务, 耗时{end - start}")