that-depends icon indicating copy to clipboard operation
that-depends copied to clipboard

Shared, and exported `create_container_lifespan` for frameworks

Open vrslev opened this issue 11 months ago • 1 comments

It would be a (small) DX boost not to have to write lifespan yourself. Applicable for FastAPI, Litestar and FastStream.

def create_container_lifespan(*containers: that_depends.BaseContainer):
    @contextlib.asynccontextmanager
    async def lifespan(*_args: typing.Any, **kwargs: typing.Any) -> typing.AsyncIterator[None]:
        try:
            yield
        finally:
            for one_container in containers:
                await one_container.DIContainer.tear_down()
    return lifespan

vrslev avatar Jan 28 '25 19:01 vrslev

Maybe it can be useful

lesnik512 avatar Feb 02 '25 13:02 lesnik512