fastapi-deferred-init
The Problem
When using nested routers in a FastAPI project its start-up time can get long quite fast. That is because every router re-calculates the routes defined by a nested router when including it and the pre-calculated values by the nested router never get used. In short: values in nested routers are calculated although they will never be used.
The Solution
This library provides a modified APIRoute that defers the calculation of values to the first actual attribute access. A router which uses the route as a default is also provided.
Usage
Older versions of this library worked by simply using custom APIRoute and APIRouter classes, but since FastAPI version 0.137.0 the optimizations require monkey patching to take effect. The following example shows how the patches can be applied by creating an asgi.py file as your entrypoint:
# my_app/asgi.py
from fastapi_deferred_init import apply_patch
# important! don't import anything else before this to make sure the patch works as expected
apply_patch()
# run your app with i.e. gunicorn my_app.asgi:app
from my_app.app import app
# mark app as re-export
__all__ = ["app"]
Caveat
When using the deferred APIRoute on every layer of the app, startup errors are deferred to the first time a route is called. So errors related to route registration might go undetected if the route is not tested at least once.
Pull Request
I created a pull request to merge this change back into FastAPI: https://github.com/tiangolo/fastapi/pull/10589 It was closed since major refactors to FastAPI are underway, but I might re-open it once the maintainers are ready.
Metadata
Release files for fastapi-deferred-init 0.3.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_deferred_init-0.3.4.tar.gz | 13.3 kB | Without provenance | Details for fastapi_deferred_init-0.3.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_deferred_init-0.3.4-py3-none-any.whl | Python 3 | none | any | Details for fastapi_deferred_init-0.3.4-py3-none-any.whl |
Total release size: 21.6 kB
Release files / fastapi_deferred_init-0.3.4.tar.gz
| Download URL | fastapi_deferred_init-0.3.4.tar.gz |
|---|---|
| Size | 13.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f7d795acaf6d846e7ea80a6300c407d755794251c3368ea6b084bdfebb25ba99
|
|
BLAKE2b-256 checksum How to use checksums |
4576254748d74ceb72b28dfa0668064a7a1a0655d2625499bad13862e0c28646
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
pdm/2.29.5 CPython/3.11.17 Linux/7.0.0-1017-azure
|
Release files / fastapi_deferred_init-0.3.4-py3-none-any.whl
| Download URL | fastapi_deferred_init-0.3.4-py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7110956178923f05250c10a1191db4ae32f8c312b114df2b8f9c4d564fd2c54e
|
|
BLAKE2b-256 checksum How to use checksums |
6731bde08d01f75352e8a42305432711b91474f31d057959068bf5e0d27c0edb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
pdm/2.29.5 CPython/3.11.17 Linux/7.0.0-1017-azure
|
Security for fastapi-deferred-init 0.3.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.