[24.1] Don't use async def
where not appropriate
#18944
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Anywhere we interact with the database or the file system or we make outgoing calls using functions that are not awaitable we shouldn't use
async def
. So basically very, very few API routes that just return in-memory data can useasync def
.If in doubt, use a normal sync definition. async dependencies with sync routes are ok (see 7b54d64 as an example for how to make it work if you do have something that needs to be awaited but you're also making blocking calls), FastAPI will run the async dependencies in the main async loop and dispatch any sync dependency to a threadpool. However async routes will run straight on the event loop, and I/O will block the event loop. If the event loop is blocked all other requests will stall, and on top of that the gunicorn liveness probe will likely time out, resulting in https://sentry.galaxyproject.org/share/issue/a04cd4cea5fb45dc9594fc7d1215b1c0/, which will eventually kill the worker and all requests currently in flight.
Of course other things can make the request arbiter decide to stop the worker, but what I've done here should be correct. All of these routes either query the database (directly or through lazy loading attributes) or touch files on the file system.
How to test the changes?
(Select all options that apply)
License