python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

High CPU usage and poor performance in FastAPI with python-dependency-injector due to SQLAlchemy engine recreation

Open Fred7b opened this issue 5 months ago • 0 comments
trafficstars

Description

Problem

Our FastAPI application with python-dependency-injector is experiencing high CPU usage (100%+) and poor performance under load.

Example Implementation

class Container(containers.DeclarativeContainer):
    config = providers.Configuration()

    db = providers.Singleton(
        Database, 
        config.sqlalchemy_database_url,
        echo=config.postgres_echo,
        application_name=config.postgres_application_name,
        is_bouncer=config.postgres_pg_bouncer
    )
    
    my_repo = providers.Singleton(
        MyProfileRepository,
        async_session=db.provided.async_session,
    )
    
    user_service = providers.Singleton(
        UserService,
        users_repository=my_repo,
    )


@router.post('/hello', tags=['login'])
@inject
async def login(
    user_service: UserService = Depends(Provide[Container.user_service]),
):
    return await user_service.hello()


def app():
    container: Container = Container()
    container.wire(
        modules=[__name__],
        keep_cache=True
        )
    app: FastAPI = FastAPI()

Performance Issues

  • Dependency injection overhead: 400ms+ per request
  • CPU utilization: 100%+ under load

Profiling Results

Using pyinstruments profiling, we identified the bottlenecks:

  • _patched in dependency_injector/wiring.py taking 400ms+
Image

Environment

  • FastAPI 0.103+
  • python-dependency-injector 4.48.0
  • SQLAlchemy 2.0+
  • asyncpg
  • Python 3.12

Fred7b avatar Jun 19 '25 14:06 Fred7b