Skip to content

Commit

Permalink
Merge pull request #155 from MOV-AI/dev
Browse files Browse the repository at this point in the history
Ver 2.4.1.41
  • Loading branch information
engdorm authored Aug 22, 2023
2 parents 616f049 + 558d163 commit 54f63b0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
6 changes: 3 additions & 3 deletions backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion backend/endpoints/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down

0 comments on commit 54f63b0

Please sign in to comment.