-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
⚡️ Parallelize dependency resolution #13756
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
base: master
Are you sure you want to change the base?
Conversation
@@ -29,7 +29,7 @@ async def get_sub_counter( | |||
|
|||
@app.get("/sub-counter-no-cache/") | |||
async def get_sub_counter_no_cache( | |||
subcount: int = Depends(super_dep), | |||
subcount: int = Depends(dep_counter), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here I had to change this to the topmost dep, because one level lower already resolves just a tiny bit later and in that case this test is not valid anymore.
3add1cd
to
21be878
Compare
This comment was marked as outdated.
This comment was marked as outdated.
cd23ae6
to
4400b4d
Compare
Tested this in a medium side production backend with fastapi setup. Found several issues, more notably around caching and exception handling. Fixed these, however these warrant for additional test cases. |
ensure_cache(None) | ||
return sub_dependant.name, None, exc.errors, None | ||
except BaseException as resolution_exc: | ||
ensure_cache(resolution_exc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests case needed.
fastapi/dependencies/utils.py
Outdated
for name, result, sub_errors, sub_exception in results: | ||
# Ensures order of exception based on dependency order. | ||
if sub_exception: | ||
raise sub_exception |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests case needed.
de0a117
to
c39fd8d
Compare
This resolves a serious performance issue in fastapi: async dependencies resolving sequentially. #639
I have attempted to pick up this previous work #3902 but I thought it was too complicated. So I am giving it a more simpler localized approach. Two things to mention:
Testing wise I'd need suggestions. Not sure how to test this and I also expect that many tests already cover the changes. Asserting that things are done in parallel would be beneficial but I'd need help with that.
EDIT:
Added in the meantime an option, just like cache, on the dependency definition called parallelizable. This is by default opt-out style, meaning it is True. Maybe to prevent breaking changes we can ship this with opt-in style. Let community experiment with it and in time if no issues are creeping up it can be considered to switch to opt-out style with a major release. For Security however I am thinking that maybe we should have it always False.