Skip to main content

Async lifecycle management with declarative dependency injection

Project description

exit-stack-container

Async lifecycle management with declarative dependency injection for Python 3.13+.

Automatically initialize and cleanup application resources (database connections, caches, clients) when your app starts and stops using async context manager pattern.

Installation

pip install exit-stack-container

Quick Start

from msgspec import Struct
from msgspec_settings import BaseSettings

from exit_stack_container import AsyncExitStackContainer, BaseResources, Dependency, on_exit


class Database:
    def __init__(self, host: str, port: int):
        print(f"Database connected to {host}:{port}")

    def close(self):
        print("Database connection closed")


@on_exit(lambda db: db.close)
def create_database(*, host: str, port: int) -> Database:
    return Database(host=host, port=port)


class DatabaseSettings(Struct, frozen=True):
    host: str = "localhost"
    port: int = 5432

class AppSettings(BaseSettings):
    database: DatabaseSettings

class AppResources(BaseResources[AppSettings]):
    database: Database

class AppContainer(AsyncExitStackContainer[AppSettings, AppResources]):
    _settings: AppSettings = AppSettings()

    database: Dependency = Dependency(
        create_database,
        host=_settings.database.host,
        port=_settings.database.port,
    )


# Use with async context manager - automatic init and cleanup
async def main():
    async with AppContainer() as resources:
        # resources.database is ready to use
        # cleanup happens automatically on exit
        pass

Features

  • Declarative DI — Define dependencies with type-safe descriptors
  • Async lifecycle — Automatic resource initialization and cleanup via async context manager
  • Topological sorting — Dependencies resolved in correct order automatically
  • Circular detection — Prevents circular dependency issues at runtime
  • Single active context — Container cannot be re-entered before exiting

License

MIT

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

exit_stack_container-0.1.2.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

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

exit_stack_container-0.1.2-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file exit_stack_container-0.1.2.tar.gz.

File metadata

  • Download URL: exit_stack_container-0.1.2.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for exit_stack_container-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4b85340b43624c1231b3f7dd8f8ce9f155fac836472248bdab8bfba7afe7bb21
MD5 b0b69118c783556828a6b76eb76d8548
BLAKE2b-256 354e669a25862554f3cd7adf65ef2165562ea4f6ae0ba8815a2d52ef67becf94

See more details on using hashes here.

File details

Details for the file exit_stack_container-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for exit_stack_container-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 15d547bb55875e76645d08db5ab1b3e03173366b8f7dac8595ca28f73db42239
MD5 6334b4f84784692ea70b8badd9919f25
BLAKE2b-256 e133b5e7decef0d1ff72c4c26333ad8d95cb49418a4c3cc41fcbdf4a854bcd8f

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