Skip to main content

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

fastapi_lifespan_manager-0.1.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_lifespan_manager-0.1.3-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_lifespan_manager-0.1.3.tar.gz.

File metadata

  • Download URL: fastapi_lifespan_manager-0.1.3.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-1018-azure

File hashes

Hashes for fastapi_lifespan_manager-0.1.3.tar.gz
Algorithm Hash digest
SHA256 a8be3c95a3c38db4f90d185eb0bd07c2db176766311f3633c43a7787d9528172
MD5 948f7b5c6ba2650ffaef2e2017a29d79
BLAKE2b-256 b8ed4d24e25f8d3640308c4689a28d44b271025ee721ea0f07305bb2bad015ac

See more details on using hashes here.

File details

Details for the file fastapi_lifespan_manager-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_lifespan_manager-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 285a6da8bd9d2f7c3f5192259ad07dc0426b6b1863b23a1e1d3a3f1c51ac2f9f
MD5 446eda5074fe696eb2c9709988565358
BLAKE2b-256 b5df9b472737ec94b3aafdb02c5ccedc0417891ab34dd669163eb8c6eff77987

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page