FastAPI Lifespan Manager
Project description
Fastapi LifespanManager
fastapi-lifespan-manager
is a Python library that provides a lifespan manager for FastAPI applications.
The LifespanManager
in fastapi-lifespan-manager
allows you to have multiple lifespan in one
application.
This library is particularly useful for managing background tasks, such as starting and stopping a database connection or managing a cache, as well as for performing cleanup tasks, such as closing open file handles or shutting down running processes.
To use fastapi-lifespan-manager
, simply install it via pip:
pip install fastapi-lifespan-manager
Usage Example:
from typing import AsyncIterator
from fastapi import FastAPI
from redis.asyncio import Redis
from sqlalchemy.ext.asyncio import create_async_engine
from fastapi_lifespan_manager import LifespanManager, State
manager = LifespanManager()
@manager.add
async def setup_db(app: FastAPI) -> AsyncIterator[State]:
engine = await create_async_engine("postgresql+asyncpg://user:password@localhost/db")
yield {"db": engine}
await engine.dispose()
@manager.add
async def setup_cache(app: FastAPI) -> AsyncIterator[State]:
redis = await Redis.from_url("redis://localhost:6379/0")
yield {"cache": redis}
await redis.close()
await redis.wait_closed()
app = FastAPI(lifespan=manager)
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
Built Distribution
File details
Details for the file fastapi_lifespan_manager-0.1.4.tar.gz
.
File metadata
- Download URL: fastapi_lifespan_manager-0.1.4.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.8.18 Linux/6.2.0-1019-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1dc4a776cd4305bf7baf4791f148216c21ab966916216d72b5dc2a27a21d8d02 |
|
MD5 | a92ee541e55592340564ea0580b21dc1 |
|
BLAKE2b-256 | 061ae559faa77de5d4afa02076de8257bcd492a9ea8c9206f2b3e5e451933349 |
File details
Details for the file fastapi_lifespan_manager-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: fastapi_lifespan_manager-0.1.4-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.8.18 Linux/6.2.0-1019-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d18e2c01cd91432ed05c7f94a02bb43e9ad68a5a55ccc813005f0b5f79e8bd9 |
|
MD5 | a3695cc6b73203237d42515fa1216050 |
|
BLAKE2b-256 | 5682755d5092478a2cdec3528a2b7e4594af1fcc231cabfbbad492be99687191 |