I run the following program:
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"}
with uvicorn. Then I open in browser http://127.0.0.1:8000/ in two different tabs in a short period of time. The output is like this:
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
Why does the second Start go before first Executed even though root
is not async
?