-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
Application factory pattern is slow because of include_router
, allow passing top level router FastAPI(router=router)
#5343
Comments
The motivation here seems the test suite. Why not using a session fixture instead? |
I would like a clean application for every test, a session fixture would mean the same app instance every time. |
How do we add multiple routers in an app? |
|
I'm asking this in context of this issue. They are suggesting to pass the router in the |
@adarshpunj this is an enhancement request, it is not yet possible to pass a router object to the application object. |
Thanks for the suggestion, but as that would mean another parameter, more maintenance, etc. And it's not something that I've seen need by anyone else, and you already solved your use case, I'll pass on this for now. I also plan on refactoring how including routers work, that will probably also change all this. Thanks everyone for the discussion here! |
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs. |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First Check
Commit to Help
Example Code
Description
We are using the application factory pattern with FastAPI as it allows us not to have the app get configured at import time. This works well for us and allows us to test the application easier.
During testing each test that needs the application gets a fresh instance by calling
create_app
in a pytest fixture. In an effort to speed up our test suite we performed some profiling to find the most expensive calls inside ourcreate_app
function. It turns out it wasinclude_router
, I guess its because it does a bunch of introspection and checks for each route.After some experiments we were able to take our test suite from 2 minutes down to 15 seconds by doing this:
This basically manually sets the router and rebuilds the middleware stack to avoid calling
include_router
again. This definitely seems like a hack but hey it works for us at the moment.Perhaps it is a good idea to allow passing in a "top level" router to the
FastAPI()
constructor that would do this for us?Wanted Solution
Possible solutions:
include_router
) is completed already.include_router
really fastWanted Code
Alternatives
No response
Operating System
Linux, Windows, macOS
Operating System Details
No response
FastAPI Version
0.81.0
Python Version
3.9.9
Additional Context
No response
The text was updated successfully, but these errors were encountered: