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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file exit_stack_container-0.1.0.tar.gz.
File metadata
- Download URL: exit_stack_container-0.1.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44c6c63a9164fd376b682f2c042cbcb4c8c73d02a6bef57404560ad926090855
|
|
| MD5 |
38f95223286f14c11be985ccbdecc6e9
|
|
| BLAKE2b-256 |
814a3484f0205dd02202699dcbd8db8d23f6d3a9fb9609f41f613fcc19dc376c
|
File details
Details for the file exit_stack_container-0.1.0-py3-none-any.whl.
File metadata
- Download URL: exit_stack_container-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f628d4ddac8dbf1c500cd525640a255190d2bf1a835adfc085c6bae27a7242c9
|
|
| MD5 |
496aebd2a2e4885f88e5394a61b9ceac
|
|
| BLAKE2b-256 |
96aa1979f3218e07e0234708f9929bbbc5774349f922a5277fc9dc9e79b0dee8
|