Skip to content

Commit

Permalink
#58 - Recommender only ever trained once
Browse files Browse the repository at this point in the history
- Lock should not be bound to the current thread since we use it to synchronize across threads
  • Loading branch information
reckart committed Mar 12, 2024
1 parent 409739b commit 5fad012
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ariadne/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}]')

Check warning on line 77 in ariadne/server.py

View check run for this annotation

Codecov / codecov/patch

ariadne/server.py#L77

Added line #L77 was not covered by tests
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}]')

Check warning on line 84 in ariadne/server.py

View check run for this annotation

Codecov / codecov/patch

ariadne/server.py#L84

Added line #L84 was not covered by tests
lock.release()

# We spawn a thread and run the training in there so that this HTTP request can return directly
Expand All @@ -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)

Check warning on line 97 in ariadne/server.py

View check run for this annotation

Codecov / codecov/patch

ariadne/server.py#L97

Added line #L97 was not covered by tests

0 comments on commit 5fad012

Please sign in to comment.