-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using fast-api non-async libraries #260
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 |
@dmontagu Thanks for the clearing things up for me! |
Excellent answer @dmontagu ! Thanks for helping here. Thanks @codingsett for reporting back and closing the issue. |
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 |
@dmontagu @tiangolo what if we want to do a blocking call in an async function? |
Hey @jin09, I built Asyncer for that 🤓 https://asyncer.tiangolo.com/
|
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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.
The text was updated successfully, but these errors were encountered: