diff --git a/backend/__init__.py b/backend/__init__.py index 35c2c57..d5c43a9 100644 --- a/backend/__init__.py +++ b/backend/__init__.py @@ -10,7 +10,8 @@ """ import os -from concurrent.futures import ProcessPoolExecutor +from concurrent.futures import ThreadPoolExecutor + from aiohttp import web from dal.data.shared.vault import JWT_SECRET_KEY @@ -28,7 +29,6 @@ HTTP_HOST = os.getenv("HTTP_HOST", "0.0.0.0") HTTP_PORT = int(os.getenv("HTTP_PORT", "5004")) - async def log_streamer(app: web.Application): """ This function is made for context handling by aiohttp. @@ -78,7 +78,7 @@ def main(): # initialize web app, this is the main/parent application # APIs and other applications are added as sub applications main_app = web.Application() - main_app["executor"] = ProcessPoolExecutor(max_workers=2) + main_app["executor"] = ThreadPoolExecutor(max_workers=10) main_app.on_response_prepare.append(on_prepare) # prepare JWT middleware diff --git a/backend/endpoints/auth.py b/backend/endpoints/auth.py index f79483d..30385f3 100644 --- a/backend/endpoints/auth.py +++ b/backend/endpoints/auth.py @@ -100,7 +100,7 @@ async def post_token_auth(self, request: web.Request) -> web.Response: output = {} status = 200 try: - asyncio.create_task(TokenManager.remove_all_expired_tokens()) + await self._run_blocking_code(request, TokenManager.remove_all_expired_tokens) data = await request.json() domain = data["domain"].lower() @@ -247,5 +247,19 @@ def get_domains(self, request: web.Request) -> web.Response: output["domains"] = AUTH_MANAGER.get_domains() return web.json_response(output, headers={"Server": "Movai-server"}) + async def _run_blocking_code(self, request: web.Request, func: callable, *args) -> any: + """Runs a blocking function that may take long time. + + Args: + func (callable): The function to run. + + Returns: + Any: The return value of the function. + """ + executor = request.app["executor"] + loop = asyncio.get_event_loop() + future = loop.run_in_executor(executor, func, *args) + await future + WebAppManager.register("/auth/", AuthApp) diff --git a/setup.py b/setup.py index edfbd32..33375ec 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ "miracle-acl==0.0.4.post1", "PyYAML==6.0", "requests==2.22.0", - "movai-core-shared==2.4.1.24", + "movai-core-shared==2.4.1.26", "data-access-layer==2.4.1.35", "gd-node==2.4.1.17", ]