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 resources (database, cache, clients) using async context managers.
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}")
async def close(self):
print("Database connection closed")
async def query(self, query: str):
print(f"Executing query: {query}")
class Usecase:
def __init__(self, database: Database):
self.database = database
print(f"Usecase created with database at {database}")
async def __call__(self):
await self.database.query("SELECT * FROM table")
@on_exit(lambda db: db.close)
async def create_database(*, host: str, port: int) -> Database:
return Database(host=host, port=port)
def create_usecase(*, database: Database) -> Usecase:
return Usecase(database=database)
class DatabaseSettings(Struct, frozen=True):
host: str = "localhost"
port: int = 5432
class AppSettings(BaseSettings):
database: DatabaseSettings
class AppResources(BaseResources[AppSettings]):
usecase: Usecase
class AppContainer(AsyncExitStackContainer[AppSettings, AppResources]):
_settings: AppSettings = AppSettings()
database: Dependency = Dependency(
create_database,
host=_settings.database.host,
port=_settings.database.port,
)
usecase: Dependency = Dependency(
create_usecase,
database=database,
)
async def main():
async with AppContainer() as resources:
await resources.usecase()
await main()
Output:
Database connected to localhost:5432
Usecase created with database at <__main__.Database object at 0x7f4669422120>
Executing query: SELECT * FROM table
Database connection closed
Features
- Declarative DI — Type-safe dependency descriptors
- Async lifecycle — Automatic init and cleanup via context managers
- Topological sorting — Correct dependency resolution order
- Circular detection — Prevents circular dependency issues
- Single active context — Cannot re-enter before exit
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.3.tar.gz.
File metadata
- Download URL: exit_stack_container-0.1.3.tar.gz
- Upload date:
- Size: 26.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c12c1c54625bc74346ef196b27775fb0d8ec8366c59920c000fc52f13cd3c5c
|
|
| MD5 |
16b58576e370d1f887acec945339c743
|
|
| BLAKE2b-256 |
a10cc8cbc2da4c19298d3cb0df69e13cbdbb9fef96ef8e02f006ba27039ad941
|
File details
Details for the file exit_stack_container-0.1.3-py3-none-any.whl.
File metadata
- Download URL: exit_stack_container-0.1.3-py3-none-any.whl
- Upload date:
- Size: 9.7 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 |
3e1922ac592bfef59e72ceaa3a86b9fe7186e8fa9e9accac1118272c9e6214fb
|
|
| MD5 |
57552f151b36139c408cc8ab08fd4963
|
|
| BLAKE2b-256 |
4384adbf1914ed0a3e98035f9ab9b887895079efd62b23d6fda3a64c4b32ee9f
|