fastapi-lifespan-manager
FastAPI Lifespan Manager
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-manageruv
uv add fastapi-lifespan-managerpoetry
poetry add fastapi-lifespan-managerInstalling 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
Tags
More Application Frameworks packages
FastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
textualTextual is a Python framework for building…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
mcpBuild and connect to Model Context Protocol…
permissive · top 1,000 on PyPI
WerkzeugWerkzeug is a WSGI utility library providing…
permissive · top 1,000 on PyPI
asgi-lifespanProgrammatically trigger ASGI application…
permissive · top 5,000 on PyPI
fastapi-cache2Caches FastAPI endpoint and function results…
permissive · top 5,000 on PyPI
starsessionsProvides session management middleware for…
permissive · top 15,000 on PyPI
fastapi-utilitiesProvides reusable utilities for FastAPI…
permissive · top 15,000 on PyPI
fastapi-cliFastAPI CLI is a command-line tool that runs…
permissive · top 1,000 on PyPI
fastapi-injectableEnables FastAPI's Depends() dependency…
permissive · top 15,000 on PyPI
FastAPI-SQLAlchemyProvides middleware and helpers to integrate…
permissive · top 15,000 on PyPI
taskiq-fastapiIntegrates FastAPI request and dependency…
permissive · top 15,000 on PyPI
nc-py-apiPython client library for interacting with…
permissive · top 5,000 on PyPI
growthbookGrowthBook is a feature flagging and A/B…
permissive · top 5,000 on PyPI