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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_deferred_init-0.3.2.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_deferred_init-0.3.2-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_deferred_init-0.3.2.tar.gz.

File metadata

  • Download URL: fastapi_deferred_init-0.3.2.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.28.0 CPython/3.10.20 Linux/6.17.0-1020-azure

File hashes

Hashes for fastapi_deferred_init-0.3.2.tar.gz
Algorithm Hash digest
SHA256 190b469480fac1fb3cfbb9f699e6ed4e20cfad0f5417cf9ab06cec8fd50cc2d7
MD5 094c5d15487a9935b8bd15ef5c873a76
BLAKE2b-256 87610d43764931a2b9a6695b33c6c7c8b8597b935497b7fdf80a8111200aa852

See more details on using hashes here.

File details

Details for the file fastapi_deferred_init-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_deferred_init-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e45632d1980df68b9f24bf8a93c3698afcc9e1d09d28fcfcb888ed92da9cfdf5
MD5 37aa8df8535fa6c851e8a1d516a0a018
BLAKE2b-256 daccc6259e4bfe31402735c2962328a128f20c829b7e252410691a42c9543b91

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 files

0.3.1

2 files

0.3.0

2 files

0.2.12

2 files

0.2.11

2 files

0.2.10

2 files

0.2.9

2 files

0.2.8

2 files

0.2.7

2 files

0.2.6

2 files

0.2.5

2 files

0.2.4

2 files

0.2.3

2 files

0.2.2.post1

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page