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.1.tar.gz (9.6 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.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_deferred_init-0.3.1.tar.gz
  • Upload date:
  • Size: 9.6 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.1.tar.gz
Algorithm Hash digest
SHA256 1d3f669264d2f6e517194e303a1cae09bf8328a73e498d05d9cd714965d3ebde
MD5 8423c24dd7ed66d26db1586a7304f6bd
BLAKE2b-256 e08492ec8c0c42dcfe502bf9f3d201ba25ece30b407da874e1dea4d5c5495975

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_deferred_init-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4eac67241f0560e5dc90522e95f4e10626e0a3e0d74120a26b83d5e5e9073878
MD5 c624c2242fa5774486fb308b052f20da
BLAKE2b-256 af7b1d78115a22455ffc85670545e7fe5ee4c413ac4c4e46698ae8219bc8e1d8

See more details on using hashes here.

Release history Release notifications | RSS feed

0.3.2

2 files

This release

0.3.1 This release

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