0

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?

Amin Ba
  • 1,603
  • 1
  • 13
  • 38

1 Answers1

1

From the Fast API doc:

When you declare a path operation function with normal def instead of async def, it is run in an external threadpool that is then awaited, instead of being called directly (as it would block the server).

In Fast API, sync callables behave similarly to async tasks using a threadpool executor (reference doc for awaiting a sync callable in a thread).

specbug
  • 512
  • 5
  • 16