我运行以下程序:
import time
from datetime import datetime
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
print(f"Started at {datetime.now()}")
time.sleep(30)
print(f"Executed at {datetime.now()}")
return {"message": "Hello World"}
与独角兽。 然后我在短时间内在两个不同的选项卡中打开浏览器http://127.0.0.1:8000/。输出是这样的:
Started at 2023-04-01 23:40:53.668811
Started at 2023-04-01 23:41:16.992891
Executed at 2023-04-01 23:41:23.779460
INFO: 127.0.0.1:54310 - "GET / HTTP/1.1" 200 OK
Executed at 2023-04-01 23:41:47.248950
INFO: 127.0.0.1:54311 - "GET / HTTP/1.1" 200 OK
即使
root
不是异步的,为什么第二个 Start 在第一个 Executed 之前进行?