I have this small fastapi application
# run_sync_8001.py
import time
import uvicorn
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/")
def sleep(n: int = Query()):
    time.sleep(n)
    return "Done"
def main():
    uvicorn.run(
        "run_sync_8001:app",
        host="0.0.0.0",
        reload=True,
        port=8001,
        workers=1
    )
if __name__ == "__main__":
    main()
I use Postman and send these three requests one after another very fast:
curl --location 'http://127.0.0.1:8001/?n=10'
curl --location 'http://127.0.0.1:8001/?n=1'
curl --location 'http://127.0.0.1:8001/?n=1'
I expect the last one should take 12 seconds but it is taking less than a second.
I would expect that behaviour from this app instead
# run_async_8002.py
import asyncio
import uvicorn
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/")
async def sleep(n: int = Query()):
    await asyncio.sleep(n)
    return "Done"
def main():
    uvicorn.run(
        "run_async_8002:app",
        host="0.0.0.0", reload=True,
        port=8002,
        workers=1
)
if __name__ == "__main__":
    main()
when sending:
curl --location 'http://127.0.0.1:8002/?n=10'
curl --location 'http://127.0.0.1:8002/?n=1'
curl --location 'http://127.0.0.1:8002/?n=1'
How are async and sync different then?
 
    