From 9cecb477cbf1e03c12d2e5723d921d6a829765b5 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Mon, 29 Jun 2020 09:03:27 +0100 Subject: [PATCH] Flipped to explicit finite lock timeouts --- .../api/default_extensions/autofocus.py | 10 +++--- .../api/default_extensions/scan.py | 35 ++++++++++--------- .../api/v2/views/actions/stage.py | 8 +++-- openflexure_microscope/camera/base.py | 2 +- openflexure_microscope/microscope.py | 8 ++--- openflexure_microscope/stage/base.py | 2 +- 6 files changed, 35 insertions(+), 30 deletions(-) diff --git a/openflexure_microscope/api/default_extensions/autofocus.py b/openflexure_microscope/api/default_extensions/autofocus.py index 84ccb76d..d61f72df 100644 --- a/openflexure_microscope/api/default_extensions/autofocus.py +++ b/openflexure_microscope/api/default_extensions/autofocus.py @@ -376,10 +376,12 @@ class FastAutofocusAPI(ActionView): if microscope.has_real_stage(): logging.debug("Running autofocus...") - # return a handle on the autofocus task - return fast_up_down_up_autofocus( - microscope, dz=dz, mini_backlash=backlash - ) + # Acquire microscope lock with 1s timeout + with microscope.lock(timeout=1): + # Run fast_up_down_up_autofocus + return fast_up_down_up_autofocus( + microscope, dz=dz, mini_backlash=backlash + ) else: abort(503, "No stage connected. Unable to autofocus.") diff --git a/openflexure_microscope/api/default_extensions/scan.py b/openflexure_microscope/api/default_extensions/scan.py index e7e1c84d..65db21b9 100644 --- a/openflexure_microscope/api/default_extensions/scan.py +++ b/openflexure_microscope/api/default_extensions/scan.py @@ -325,22 +325,23 @@ class TileScanAPI(ActionView): logging.info("Running tile scan...") - # return a handle on the scan task - return scan_extension_v2.tile( - microscope, - basename=args.get("filename"), - temporary=args.get("temporary"), - stride_size=args.get("stride_size"), - grid=args.get("grid"), - style=args.get("style"), - autofocus_dz=args.get("autofocus_dz"), - use_video_port=args.get("use_video_port"), - resize=resize, - bayer=args.get("bayer"), - fast_autofocus=args.get("fast_autofocus"), - annotations=args.get("annotations"), - tags=args.get("tags"), - ) - + # Acquire microscope lock with 1s timeout + with microscope.lock(timeout=1): + # Run scan_extension_v2 + return scan_extension_v2.tile( + microscope, + basename=args.get("filename"), + temporary=args.get("temporary"), + stride_size=args.get("stride_size"), + grid=args.get("grid"), + style=args.get("style"), + autofocus_dz=args.get("autofocus_dz"), + use_video_port=args.get("use_video_port"), + resize=resize, + bayer=args.get("bayer"), + fast_autofocus=args.get("fast_autofocus"), + annotations=args.get("annotations"), + tags=args.get("tags"), + ) scan_extension_v2.add_view(TileScanAPI, "/tile", endpoint="tile") diff --git a/openflexure_microscope/api/v2/views/actions/stage.py b/openflexure_microscope/api/v2/views/actions/stage.py index 14a5ceed..0b9d1372 100644 --- a/openflexure_microscope/api/v2/views/actions/stage.py +++ b/openflexure_microscope/api/v2/views/actions/stage.py @@ -45,8 +45,8 @@ class MoveStageAPI(ActionView): # Move if stage exists if microscope.stage: - # Explicitally acquire lock - with microscope.stage.lock: + # Explicitally acquire lock with 1s timeout + with microscope.stage.lock(timeout=1): microscope.stage.move_rel(position) else: logging.warning("Unable to move. No stage found.") @@ -62,7 +62,9 @@ class ZeroStageAPI(ActionView): Does not move the stage, but rather makes the current position read as [0, 0, 0] """ microscope = find_component("org.openflexure.microscope") - microscope.stage.zero_position() + + with microscope.stage.lock(timeout=1): + microscope.stage.zero_position() # TODO: Make schema for microscope state return microscope.state["stage"] diff --git a/openflexure_microscope/camera/base.py b/openflexure_microscope/camera/base.py index 84b060e9..9d2c197d 100644 --- a/openflexure_microscope/camera/base.py +++ b/openflexure_microscope/camera/base.py @@ -23,7 +23,7 @@ class BaseCamera(metaclass=ABCMeta): self.thread = None self.camera = None - self.lock = StrictLock(name="Camera", timeout=1) + self.lock = StrictLock(name="Camera", timeout=None) self.frame = None self.last_access = 0 diff --git a/openflexure_microscope/microscope.py b/openflexure_microscope/microscope.py index 07f9f8d4..6672c4cb 100644 --- a/openflexure_microscope/microscope.py +++ b/openflexure_microscope/microscope.py @@ -153,7 +153,7 @@ class Microscope: Return: dict: Dictionary containing complete microscope state """ - with self.lock(timeout=None): + with self.lock: state = {"camera": self.camera.state, "stage": self.stage.state} return state @@ -161,7 +161,7 @@ class Microscope: """ Applies a settings dictionary to the microscope. Missing parameters will be left untouched. """ - with self.lock(timeout=None): + with self.lock: logging.debug("Microscope: Applying settings: {}".format(settings)) # If attached to a camera @@ -206,7 +206,7 @@ class Microscope: "extensions": self.extension_settings, } - with self.lock(timeout=None): + with self.lock: # If attached to a camera if self.camera: settings_current_camera = self.camera.read_settings() @@ -254,7 +254,7 @@ class Microscope: @property def configuration(self): - with self.lock(timeout=None): + with self.lock: initial_configuration = self.configuration_file.load() current_configuration = { diff --git a/openflexure_microscope/stage/base.py b/openflexure_microscope/stage/base.py index 3121c1bc..dcf89618 100644 --- a/openflexure_microscope/stage/base.py +++ b/openflexure_microscope/stage/base.py @@ -11,7 +11,7 @@ class BaseStage(metaclass=ABCMeta): """ def __init__(self): - self.lock = StrictLock(name="Stage", timeout=1) + self.lock = StrictLock(name="Stage", timeout=None) @abstractmethod def update_settings(self, config: dict):