Skip to content

Shared, and exported create_container_lifespan for frameworks #149

@vrslev

Description

@vrslev

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions