--- id: fastapi-lifespan-manager version: "0.1.4" license: MIT license_treatment: permissive maintenance: active --- # fastapi-lifespan-manager — FastAPI Lifespan Manager License: permissive · Maintenance: active · Downloads: 100.5K/mo ## 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 above — 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 pip install fastapi-lifespan-manager uv add fastapi-lifespan-manager 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_current - Install friction: low - Maintenance: active - Downloads: 100.5K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags fastapi lifespan management, multiple fastapi startup handlers, fastapi resource lifecycle, fastapi database connection startup, fastapi background task initialization, fastapi cleanup on shutdown, fastapi lifespan context manager, fastapi-ecosystem, lifecycle-management, async-resource-handling [View on SkillFed](https://skillfed.io/packages/fastapi-lifespan-manager) · [View on PyPI](https://pypi.org/project/fastapi-lifespan-manager/)