3

I have two scripts:

from fastapi import FastAPI
import asyncio

app = FastAPI()

@app.get("/")
async def root():
    a = await asyncio.sleep(10)
    return {'Hello': 'World',}

And second one:

from fastapi import FastAPI
import time
  
app = FastAPI()

@app.get("/")
def root():
    a = time.sleep(10)
    return {'Hello': 'World',}

Please note the second script doesn't use async. Both scripts do the same, at first I thought, the benefit of an async script is that it allows multiple connections at once, but when testing the second code, I was able to run multiple connections as well. The results are the same, performance is the same and I don't understand why would we use async method. Would appreciate your explanation.

Chris
  • 18,724
  • 6
  • 46
  • 80
filtertips
  • 801
  • 3
  • 12
  • 25

2 Answers2

1

FastAPI Docs:

You can mix def and async def in your path operation functions as much as you need and define each one using the best option for you. FastAPI will do the right thing with them.

Anyway, in any of the cases above, FastAPI will still work asynchronously and be extremely fast.

Both endpoints will be executed asynchronously, but if you define your endpoint function asynchronously, it will allow you to use await keyword and work with asynchronous third party libraries

roma
  • 34
  • 2
0

What's missing from the two other answers is why in your second example, the API can still handle multiple simultaneous requests. The reason can be found in this section of the FastAPI documentation:

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).

Meaning that all your def endpoints are run in separate threads, and that's why the API can parallelize them.

So why would you want to use async at all, if this feature exists? You can read e.g. this article comparing threading and async, but in summary, threads have more overhead, and each thread is still blocked on IO (e.g. external API or database calls).

M.O.
  • 1,712
  • 1
  • 6
  • 18