Config for running in threadpool even if defined as async def #10768
-
First Check
Commit to Help
Example Code@router.post("/example")
def example_endpoint(request: Request) -> None:
body = request.body() # oh no, starlette wants me to use async/await
persist_to_db(body) # oh no, my db library wants me to use async/await
# ... this becomes ...
@router.post("/example")
async def example_endpoint(request: Request) -> None:
body = await request.body()
await persist_to_db(body) # yay everything is fine, fastapi supports this at the top-level
# in production - wait why is this not running in the thread pool?DescriptionHi! I am coming out of a production outage my company had that ultimately can be sourced back to high volume request bursts on an endpoint defined with The original author wanted to access Little did they know (or any of us for that matter), that this was the specific difference between having the endpoint run in the main thread (blocking the event loop synchronously) versus running in the thread pool. In particular, the endpoint would take that body and persist it to the DB and write to GCP Storage (neither of those libraries for us support async/await semantics). Given these interactions, the docs recommend against My feature request is around changing the API for this nuanced difference in functional behavior to an endpoint parameter. In particular, I would imagine the above code example could be written instead like: I would actually ask that It is my understanding (and affirmed by the async docs) that the use case of
Operating SystemLinux, Windows, macOS Operating System DetailsNo response FastAPI Version0.95.2 Pydantic Version1.10.6 Python Version3.10.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Also I didn't say this above, but I would be happy to take a stab at contributing this. I just want to see if I have any mistakes in my understanding of the FastAPI concurrency model / if you would be open to it. |
Beta Was this translation helpful? Give feedback.
-
|
What you're proposing, if I understand it correctly. seems to me to be a large change to the way that FastAPI operates and there's almost no chance that would be adopted. Many databases do now have async drivers including, from memory so there are likely more than just these, However, what you can do is run your blocking code in a thread pool. Something like the following: |
Beta Was this translation helpful? Give feedback.
What you're proposing, if I understand it correctly. seems to me to be a large change to the way that FastAPI operates and there's almost no chance that would be adopted. Many databases do now have async drivers including, from memory so there are likely more than just these,
motor,asyncpg, andaiomysql. I'm guessing there are async implementations for interacting with GCP and AWS as well. In general, I'd suggest trying to make your endpoints do as little work as possible and off-load long-running and variable length tasks toBackgroundTasksor some other mechanism that isn't directly within the route.However, what you can do is run your blocking code in a thread pool. Something like th…