Using fast-api non-async libraries #8313
-
Description Hello i was wondering if i make my api with fast api and add libraries which are non-async will it work the same or will i have issues. Example is a library that sends requests to an external api. I am new to async stuff. Any advice will be appreciated thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
FastAPI should have no issues making use of non-async libraries. As long as you define your endpoints properly, you can still get the performance benefits of the async event loop as long as the non-async libraries are only blocking on IO. So yes, a (non-async) library that sends requests to an external API should integrate nicely with a FastAPI-based backend. From the FastAPI docs (https://fastapi.tiangolo.com/async/):
In other words, you'll get all the performance benefits as long as any calls with blocking IO are put inside an endpoint defined with If you do have blocking IO being called in an endpoint defined with More concretely, imagine you wanted to have an endpoint that would ultimately use a synchronous search client to execute a search (something like app = fastapi.FastAPI()
@app.get("/search")
def search(q: str): # using def here and NOT async def is important
return sync_search_client.search(query=q) On the other hand, if you wrote @app.get("/search")
async def search(q: str): # async def at the start of this line is the problem
return sync_search_client.search(query=q) then FastAPI would end up calling that function on the main server thread, and would block. Note that the same holds if you call Based on this, one might be inclined to just put all endpoints inside of functions defined with |
Beta Was this translation helpful? Give feedback.
-
@dmontagu Thanks for the clearing things up for me! |
Beta Was this translation helpful? Give feedback.
-
Excellent answer @dmontagu ! Thanks for helping here. Thanks @codingsett for reporting back and closing the issue. |
Beta Was this translation helpful? Give feedback.
-
It might be worth adding this to the documentation somewhere- I've been using this library for months but had no idea about the differences in def versus async def for the view functions would impact performance. In fact I'm actually using one of the exact use cases (elasticsearch-dsl) mentioned here and am running off to do some updates :-D |
Beta Was this translation helpful? Give feedback.
-
@dmontagu @tiangolo what if we want to do a blocking call in an async function? |
Beta Was this translation helpful? Give feedback.
-
Hey @jin09, I built Asyncer for that 🤓 https://asyncer.tiangolo.com/
|
Beta Was this translation helpful? Give feedback.
FastAPI should have no issues making use of non-async libraries. As long as you define your endpoints properly, you can still get the performance benefits of the async event loop as long as the non-async libraries are only blocking on IO. So yes, a (non-async) library that sends requests to an external API should integrate nicely with a FastAPI-based backend.
From the FastAPI docs (https://fastapi.tiangolo.com/async/):
In other words, you'll get all the performance benefits as long as a…