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 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
  • One-time use — Container cannot be reused, preventing state bugs

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.1.tar.gz (9.1 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.1-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: exit_stack_container-0.1.1.tar.gz
  • Upload date:
  • Size: 9.1 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.1.tar.gz
Algorithm Hash digest
SHA256 a142e4d95431d3c3a4ab63cb98a1fef67bc48b7c03dc9e5ffc22b461ae891595
MD5 e67f587fa6edfdf89a4f4557b9ca825b
BLAKE2b-256 5b4efdcb262393a9312fb2ddcb37d8941f6a41ec2b755e6b4e62cec1f6f47476

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exit_stack_container-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4c161f2c9ab6267343eb38332dd438fa6eb760ef28a6ed4591e241a2898e1269
MD5 2766d46e8b6ef1844eaffac0524d6ef0
BLAKE2b-256 e9a2b50711bbc869387025c326db76e8364bf5d214b7a6a4c4578f93e998f307

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