Here is my trivial fastapi
app:
from datetime import datetime
import asyncio
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/delayed")
async def get_delayed():
started = datetime.now()
print(f"Starting at: {started}")
await asyncio.sleep(10)
ended = datetime.now()
print(f"Ending at: {ended}")
return {"started": f"{started}", "ended": f"{ended}"}
if __name__ == "__main__":
uvicorn.run("fastapitest.main:app", host="0.0.0.0", port=8000, reload=True, workers=2)
When I make 2 consecutive calls to it, the code in the function for the second one doesn't start executing until the first request finishes, producing an output like:
Starting at: 2021-09-17 14:52:40.317915
Ending at: 2021-09-17 14:52:50.321557
INFO: 127.0.0.1:58539 - "GET /delayed HTTP/1.1" 200 OK
Starting at: 2021-09-17 14:52:50.328359
Ending at: 2021-09-17 14:53:00.333032
INFO: 127.0.0.1:58539 - "GET /delayed HTTP/1.1" 200 OK
Given that the function is marked async
and I am await
ing the sleep
, I would expect a different output, like:
Starting at: ...
Starting at: ...
Ending at: ...
INFO: 127.0.0.1:58539 - "GET /delayed HTTP/1.1" 200 OK
Ending at: ...
INFO: 127.0.0.1:58539 - "GET /delayed HTTP/1.1" 200 OK
[for the calls I just opened up 2 browser tabs at localhost:8000/delayed ]
What am I missing?