diff --git a/duetector/service/base.py b/duetector/service/base.py index cd14fcb..4610185 100644 --- a/duetector/service/base.py +++ b/duetector/service/base.py @@ -1,5 +1,10 @@ from typing import Any, Dict, Optional +try: + from functools import cache +except ImportError: + from functools import lru_cache as cache + from fastapi import Depends from duetector.config import Configuable @@ -15,6 +20,7 @@ def __init__(self, config: Optional[Dict[str, Any]] = None, *args, **kwargs): super().__init__(config, *args, **kwargs) +@cache def get_controller(controller_type: type): def _(config: dict = Depends(get_config)) -> Controller: return controller_type(config) diff --git a/duetector/service/query/routes.py b/duetector/service/query/routes.py index 7700760..f25b889 100644 --- a/duetector/service/query/routes.py +++ b/duetector/service/query/routes.py @@ -1,4 +1,7 @@ +from asyncio import sleep + from fastapi import APIRouter, Body, Depends +from fastapi.concurrency import run_in_threadpool from duetector.service.base import get_controller from duetector.service.query.controller import AnalyzerController @@ -35,7 +38,7 @@ async def query( Query data from analyzer """ analyzer = controller.get_analyzer(analyzer_name) - trackings = analyzer.query(**query_param.model_dump()) + trackings = await run_in_threadpool(analyzer.query, **query_param.model_dump()) return QueryResult( trackings=trackings, @@ -50,8 +53,8 @@ async def query_brief( ): # type is not serializable, so we need to get analyzer without inspect type analyzer = controller.get_analyzer(analyzer_name) - + brief = await run_in_threadpool(analyzer.brief, inspect_type=False) return BriefResult( - brief=analyzer.brief(inspect_type=False), + brief=brief, analyzer_name=analyzer_name, ) diff --git a/pyproject.toml b/pyproject.toml index 51283ee..491bc31 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,6 +19,7 @@ dependencies = [ # Following are for web server "fastapi", "uvicorn[standard]", + "anyio" ] dynamic = ["version"] classifiers = [