From 54b6feacf36f7208e84ba3a93fcfbb8fc665caf7 Mon Sep 17 00:00:00 2001 From: extreme4all <40169115+extreme4all@users.noreply.github.com> Date: Tue, 26 Nov 2024 23:14:43 +0100 Subject: [PATCH] work with semaphore --- src/core/config.py | 4 ++++ src/core/fastapi/dependencies/session.py | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/core/config.py b/src/core/config.py index 3cf55757..fe4941c0 100644 --- a/src/core/config.py +++ b/src/core/config.py @@ -1,3 +1,5 @@ +import asyncio + from dotenv import find_dotenv, load_dotenv from pydantic_settings import BaseSettings @@ -13,3 +15,5 @@ class Settings(BaseSettings): settings = Settings() + +DB_SEMAPHORE = asyncio.Semaphore(100) diff --git a/src/core/fastapi/dependencies/session.py b/src/core/fastapi/dependencies/session.py index 08f50920..ecdc4ae4 100644 --- a/src/core/fastapi/dependencies/session.py +++ b/src/core/fastapi/dependencies/session.py @@ -1,9 +1,11 @@ from sqlalchemy.ext.asyncio import AsyncSession +from src.core.config import DB_SEMAPHORE from src.core.database.database import SessionFactory # Dependency to get an asynchronous session async def get_session() -> AsyncSession: - async with SessionFactory() as session: - yield session + async with DB_SEMAPHORE: # Acquire semaphore before accessing the session + async with SessionFactory() as session: + yield session # Provide the session to the calling function