|
|
@ -4,7 +4,7 @@ import logging |
|
|
|
import pandas as pd |
|
|
|
import pandas as pd |
|
|
|
from typing import Optional, Union |
|
|
|
from typing import Optional, Union |
|
|
|
from models import ModelCache |
|
|
|
from models import ModelCache |
|
|
|
from concurrent.futures import Executor, CancelledError |
|
|
|
from concurrent.futures import Executor, CancelledError, TimeoutError |
|
|
|
import asyncio |
|
|
|
import asyncio |
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger('AnalyticUnitWorker') |
|
|
|
logger = logging.getLogger('AnalyticUnitWorker') |
|
|
@ -16,26 +16,28 @@ class AnalyticUnitWorker: |
|
|
|
self.analytic_unit_id = analytic_unit_id |
|
|
|
self.analytic_unit_id = analytic_unit_id |
|
|
|
self._detector = detector |
|
|
|
self._detector = detector |
|
|
|
self._executor: Executor = executor |
|
|
|
self._executor: Executor = executor |
|
|
|
self._training_feature: asyncio.Future = None |
|
|
|
self._training_future: asyncio.Future = None |
|
|
|
|
|
|
|
|
|
|
|
async def do_train( |
|
|
|
async def do_train( |
|
|
|
self, payload: Union[list, dict], data: pd.DataFrame, cache: Optional[ModelCache] |
|
|
|
self, payload: Union[list, dict], data: pd.DataFrame, cache: Optional[ModelCache] |
|
|
|
) -> ModelCache: |
|
|
|
) -> ModelCache: |
|
|
|
self._training_feature = asyncio.get_event_loop().run_in_executor( |
|
|
|
self._training_future = self._executor.submit( |
|
|
|
self._executor, self._detector.train, data, payload, cache |
|
|
|
self._detector.train, data, payload, cache |
|
|
|
) |
|
|
|
) |
|
|
|
try: |
|
|
|
try: |
|
|
|
new_cache: ModelCache = await self._training_feature |
|
|
|
new_cache: ModelCache = self._training_future.result(timeout = config.LEARNING_TIMEOUT) |
|
|
|
return new_cache |
|
|
|
return new_cache |
|
|
|
except CancelledError as e: |
|
|
|
except CancelledError as e: |
|
|
|
return cache |
|
|
|
return cache |
|
|
|
|
|
|
|
except TimeoutError: |
|
|
|
|
|
|
|
raise Exception('Timeout ({}s) exceeded while learning'.format(config.LEARNING_TIMEOUT)) |
|
|
|
|
|
|
|
|
|
|
|
async def do_detect(self, data: pd.DataFrame, cache: Optional[ModelCache]) -> dict: |
|
|
|
async def do_detect(self, data: pd.DataFrame, cache: Optional[ModelCache]) -> dict: |
|
|
|
return self._detector.detect(data, cache) |
|
|
|
return self._detector.detect(data, cache) |
|
|
|
|
|
|
|
|
|
|
|
def cancel(self): |
|
|
|
def cancel(self): |
|
|
|
if self._training_feature is not None: |
|
|
|
if self._training_future is not None: |
|
|
|
self._training_feature.cancel() |
|
|
|
self._training_future.cancel() |
|
|
|
|
|
|
|
|
|
|
|
async def recieve_data(self, data: pd.DataFrame, cache: Optional[ModelCache]): |
|
|
|
async def recieve_data(self, data: pd.DataFrame, cache: Optional[ModelCache]): |
|
|
|
return self._detector.recieve_data(data, cache) |
|
|
|
return self._detector.recieve_data(data, cache) |
|
|
|