Skip to main content

Faster FastAPI start-up time for Projects with many nested routers

Project description

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.

Project details


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.0.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.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_deferred_init-0.3.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.27.0 CPython/3.10.20 Linux/6.17.0-1018-azure

File hashes

Hashes for fastapi_deferred_init-0.3.0.tar.gz
Algorithm Hash digest
SHA256 869e50e23852fd200236994c8b5a6b89d63093e71f6e565e8d30b0ac62fcea37
MD5 cc14631147b0586cc98e891a1b62b6c8
BLAKE2b-256 7d4cf3bda6d0e09f440ae9b4733543609b9ea91820f75129f10111e0c244032a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_deferred_init-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6ef006ef21c2544bc7151aa47582b7ca9a3cce5205b56a24fe8e147a01c33ffc
MD5 6cabf122aa669fba94326e25743cebea
BLAKE2b-256 3feb93eff175102fca20fd691dcac08a82094254036e8900f62ac5056b83699e

See more details on using hashes here.

Supported by

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