ets-labs / python-dependency-injector

Dependency injection framework for Python
https://python-dependency-injector.ets-labs.org/
BSD 3-Clause "New" or "Revised" License
3.7k stars 295 forks source link

Async mode in miniapps/fastapi-sqlalchemy does not work #674

Open mitgate opened 1 year ago

mitgate commented 1 year ago

Is there any way to turn miniapps/fastapi-sqlalchemy to async ?

Im using postgresql+asyncpg on connection

replaced all classes with async ones..

from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_scoped_session, async_sessionmaker

and the error when starting a session is

async with self.session_factory() as session: AttributeError: aenter

IniZio commented 1 year ago

I used fastapi_async_sqlalchemy instead not sure if that will help

ALeksandr-Fuze commented 1 year ago

Maybe that helps you. I do it inside the container class inherited from DeclarativeContainer (used sqlalchemy):

    db_async_session = Factory(
        async_sessionmaker,
        bind=db_engine,
        class_=AsyncSession,
        expire_on_commit=False
    )
  your_service = Factory(YourService, session=db_async_session)