Skip to main content

fastapi-deferred-init

PyPI - Downloads GitHub license Tests

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.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-deferred-init 0.3.3
File Size Uploaded
fastapi_deferred_init-0.3.3.tar.gz 9.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-deferred-init 0.3.3
File Interpreter ABI Platform
fastapi_deferred_init-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 16.9 kB

Release files / fastapi_deferred_init-0.3.3.tar.gz

Download URL fastapi_deferred_init-0.3.3.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
efb98fbd8c59ab93a204092df7f9cd359af13a38ce0d987a9dd148d6c6c83860
BLAKE2b-256 checksum
How to use checksums
861f372d7ec7a891882c14d85427cf1458373ff7ba0b78e852a4a0ab387ce87b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via pdm/2.29.0 CPython/3.10.21 Linux/6.17.0-1022-azure

Release files / fastapi_deferred_init-0.3.3-py3-none-any.whl

Download URL fastapi_deferred_init-0.3.3-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6dbe7841b0006ccbe60d259c4151b6d13995c96a63d35611805349656637b0fe
BLAKE2b-256 checksum
How to use checksums
97013e24ff8bb4f759868683c782a56c66d78510dcaff8c2f1afe754e6de58f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via pdm/2.29.0 CPython/3.10.21 Linux/6.17.0-1022-azure

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.11

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page