skillfed

fastapi-lifespan-manager

FastAPI Lifespan Manager

fastapi-lifespan-manager v0.1.4 100.5K downloads/30d#12,984 on PyPI42
Permissive license MIT Active released

What it is and what it does

fastapi-lifespan-manager wraps FastAPI's lifespan context manager to allow you to register multiple startup and shutdown handlers without nesting or manually combining them. Instead of writing a single monolithic lifespan context manager, you decorate individual async generator functions with @manager.add, each yielding a dictionary of state to attach to the app. The manager collects these handlers and runs them sequentially during application startup and shutdown.

This is particularly useful for applications that need to initialize multiple external resources—databases, caches, message queues, connection pools—where each resource has its own setup and teardown logic. Rather than manually coordinating all of these in one place, you can define each resource's lifecycle independently and let the manager orchestrate them. The package depends only on fastapi and supports Python 3.8 through 3.12.

Use it for:

  • Initialize multiple databases and caches on startup, ensuring each is properly closed when the app shuts down.
  • Manage background task workers or thread pools that need to be started at app launch and stopped cleanly.
  • Coordinate setup of external service clients (Redis, Elasticsearch, etc.) with their respective cleanup routines.
  • Separate resource initialization logic into modular, reusable handlers rather than one large lifespan function.
  • Build plugin or extension systems where each module registers its own startup and shutdown behavior.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Provides a lifespan manager for FastAPI that allows multiple startup and shutdown handlers to coexist in a single application, simplifying management of resources like database connections and caches.

Yes, if you have a FastAPI application with multiple resources that need coordinated startup and shutdown. The package is actively maintained, has no known vulnerabilities, and solves a real organizational problem—keeping lifespan logic modular and readable. The low install friction and permissive license make adoption straightforward. Not necessary for simple applications with only one or two resources.

Install

fastapi-lifespan-manager on PyPI

pip

pip install fastapi-lifespan-manager

uv

uv add fastapi-lifespan-manager

poetry

poetry add fastapi-lifespan-manager

Installing fastapi-lifespan-manager

Before you install

Low friction: pure Python wheel with only fastapi as a runtime dependency. Repository is active with recent commits and no archived status, though the package is relatively young (first release April 2023).

License in practice

MIT license is permissive; you can use, modify, and distribute this package freely in commercial and private projects with minimal restrictions.

Quickstart

pip install fastapi-lifespan-manager

from fastapi import FastAPI
from fastapi_lifespan_manager import LifespanManager

manager = LifespanManager()

@manager.add
async def setup_resource(app: FastAPI):
    # Initialize resource
    yield {"resource": resource}
    # Cleanup

app = FastAPI(lifespan=manager)

Verify before relying

  • Whether the package handles exception cases during lifespan setup or teardown gracefully
  • Performance characteristics when managing many concurrent lifespan handlers
  • Compatibility with FastAPI's native lifespan parameter in versions after 0.93

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.8,<4.0)
Install friction low — pure-Python wheel
Runtime dependencies 1 — fastapi
Maintenance actively maintained — 906 days since the last release
Last repo commit
First released
Downloads 100,501/month — #12,984 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: fastapi_lifespan_manager-0.1.4-py3-none-any.whl

License :: OSI Approved :: MIT LicenseProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9

Tags

fastapi lifespan managementmultiple fastapi startup handlersfastapi resource lifecyclefastapi database connection startupfastapi background task initializationfastapi cleanup on shutdownfastapi lifespan context manager
fastapi-ecosystemlifecycle-managementasync-resource-handling

More Application Frameworks packages