From 5fad0126c8a6526e4db2fddadf836505ce4453b8 Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Tue, 12 Mar 2024 12:02:06 +0100 Subject: [PATCH] #58 - Recommender only ever trained once - Lock should not be bound to the current thread since we use it to synchronize across threads --- ariadne/server.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ariadne/server.py b/ariadne/server.py index e661023..e595099 100644 --- a/ariadne/server.py +++ b/ariadne/server.py @@ -74,12 +74,14 @@ def _train(self, classifier_name: str): # The lock needs to be acquired out here, not in the fn scope, else it would # just throw the Timeout inside fn. lock = self._get_lock(classifier.name, user_id) + logger.debug(f'Acquiring lock for [{user_id}, {classifier.name}]') lock.acquire() def _fn(): try: classifier.fit(req.documents, req.layer, req.feature, req.project_id, user_id) finally: + logger.debug(f'Releasing lock for [{user_id}, {classifier.name}]') lock.release() # We spawn a thread and run the training in there so that this HTTP request can return directly @@ -92,4 +94,4 @@ def _fn(): def _get_lock(self, classifier_name: str, user_id: str) -> FileLock: self._lock_directory.mkdir(parents=True, exist_ok=True) lock_path = self._lock_directory / f"{classifier_name}_{user_id}.lock" - return FileLock(lock_path, timeout=1) + return FileLock(lock_path, timeout=1, thread_local=False)