场景复现

1
2
3
4
5
6
7
8
9
10
11
12
async def async_test():
time.sleep(2)
print("async_test", time.time())
return "success"


@router.post("/test", summary="test")
async def test():
task = asyncio.create_task(async_test())
# await asyncio.gather(task)
time.sleep(5)
return "success"

浏览器访问 /test 路由,会发现,在 5 秒后返回响应 “success” ,接着 async_test 会自动被执行,于 2 秒后打印日志。