Skip to content

Commit

Permalink
Replace custom training lock by filelock (asreview#1463)
Browse files Browse the repository at this point in the history
  • Loading branch information
J535D165 authored Jun 8, 2023
1 parent f966dd5 commit 7b97af6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 150 deletions.
32 changes: 9 additions & 23 deletions asreview/webapp/run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import sys
from pathlib import Path

from filelock import FileLock
from filelock import Timeout

from asreview.models.balance import get_balance_model
from asreview.models.classifiers import get_classifier
from asreview.models.feature_extraction import get_feature_model
Expand All @@ -25,7 +28,6 @@
from asreview.project import open_state
from asreview.review.base import BaseReview
from asreview.webapp.io import read_data
from asreview.webapp.sqlock import SQLiteLock


def get_lab_reviewer(
Expand Down Expand Up @@ -85,28 +87,14 @@ def train_model(project):
"""Add the new labels to the review and do the modeling.
It uses a lock to ensure only one model is running at the same time.
Old results directories are deleted after 4 iterations.
It has one argument on the CLI, which is the base project directory.
"""

logging.info(f"Project {project.project_path} - Train a new model for project")

# get file locations
lock_file = Path(project.project_path, "lock.sqlite")

# Lock so that only one training run is running at the same time.
# It doesn't lock the flask server/client.
with SQLiteLock(
lock_file, blocking=False, lock_name="training", project_id=project.project_id
) as lock:
# If the lock is not acquired, another training instance is running.
if not lock.locked():
logging.info(
f"Project {project.project_path} - "
"Cannot acquire lock, other instance running."
)
return
lock = FileLock(Path(project.project_path, "training.lock"), timeout=0)

with lock:

# Check if there are new labeled records.
with open_state(project.project_path) as state:
Expand All @@ -120,11 +108,6 @@ def train_model(project):

# Train the model.
reviewer.train()
else:
logging.info(
f"Project {project.project_path} - No new labels since last run."
)
return


def main(argv):
Expand Down Expand Up @@ -153,6 +136,9 @@ def main(argv):
# change the project status to review
project.update_review(status="review")

except Timeout:
logging.debug("Another iteration is training")

except Exception as err:
# save the error to the project
project.set_error(err, save_error_message=args.output_error)
Expand Down
127 changes: 0 additions & 127 deletions asreview/webapp/sqlock.py

This file was deleted.

0 comments on commit 7b97af6

Please sign in to comment.