Metadata-Version: 2.1
Name: fastapi-lifespan-manager
Version: 0.1.2
Summary: FastAPI Lifespan Manager
Home-page: https://github.com/uriyyo/fastapi-lifespan-manager
License: MIT
Author: Yurii Karabas
Author-email: 1998uriyyo@gmail.com
Requires-Python: >=3.8,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Dist: fastapi (>=0.93.0)
Project-URL: Repository, https://github.com/uriyyo/fastapi-lifespan-manager
Description-Content-Type: text/markdown

<h1 align="center">
Fastapi LifespanManager
</h1>

`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:

```bash
pip install fastapi-lifespan-manager
```

Usage Example:

```python
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)
```

