diff --git a/openflexure_microscope/api/app.py b/openflexure_microscope/api/app.py index 38b96646..cfe6b8f7 100644 --- a/openflexure_microscope/api/app.py +++ b/openflexure_microscope/api/app.py @@ -99,17 +99,19 @@ app.register_blueprint(plugin_blueprint, url_prefix=uri('/plugin', 'v1')) task_blueprint = blueprints.task.construct_blueprint(api_microscope) app.register_blueprint(task_blueprint, url_prefix=uri('/task', 'v1')) - # Automatically clean up microscope at exit def cleanup(): global api_microscope + logging.debug("App teardown started...") # Save config api_microscope.rc.save(backup=True) # Close down the microscope api_microscope.close() + logging.debug("App teardown complete.") atexit.register(cleanup) + if __name__ == "__main__": app.run(host='0.0.0.0', port="5000", threaded=True, debug=True, use_reloader=False) diff --git a/openflexure_microscope/camera/base.py b/openflexure_microscope/camera/base.py index 86bab7c1..2b2ae73d 100644 --- a/openflexure_microscope/camera/base.py +++ b/openflexure_microscope/camera/base.py @@ -135,12 +135,14 @@ class BaseCamera(object): def close(self): """Close the BaseCamera and all attached StreamObjects.""" + logging.info("Closing {}".format(self)) # Close all StreamObjects for capture_list in [self.images, self.videos]: for stream_object in capture_list: stream_object.close() # Stop worker thread self.stop_worker() + logging.info("Closed {}".format(self)) def wait_for_camera(self, timeout=5): """Wait for camera object, with 5 second timeout.""" @@ -159,10 +161,12 @@ class BaseCamera(object): self.last_access = time.time() self.stop = False - - if self.thread is None: + + #if self.thread is None: + if not self.state['stream_active']: # start background frame thread self.thread = threading.Thread(target=self._thread) + self.thread.daemon = True self.thread.start() # wait until frames are available @@ -171,7 +175,7 @@ class BaseCamera(object): if time.time() > timeout_time: raise TimeoutError("Timeout waiting for frames.") else: - time.sleep(0) + time.sleep(0.1) return True def stop_worker(self, timeout: int = 5) -> bool: @@ -179,12 +183,19 @@ class BaseCamera(object): logging.debug("Stopping worker thread") timeout_time = time.time() + timeout - self.stop = True - while self.thread: + #if self.thread: + if self.state['stream_active']: + self.stop = True + self.thread.join() # Wait for stream thread to exit + logging.debug("Waiting for stream thread to exit.") + + #while self.thread: + while self.state['stream_active']: if time.time() > timeout_time: + logging.debug("Timeout waiting for worker thread close.") raise TimeoutError("Timeout waiting for worker thread close.") else: - time.sleep(0) + time.sleep(0.1) return True # HANDLE STREAM FRAMES @@ -387,4 +398,5 @@ class BaseCamera(object): # Set stream_activate state self.state['stream_active'] = False # Reset thread - self.thread = None + #self.thread = None + logging.debug("Stream thread is None") diff --git a/openflexure_microscope/camera/capture.py b/openflexure_microscope/camera/capture.py index 10bfae17..37fc035e 100644 --- a/openflexure_microscope/camera/capture.py +++ b/openflexure_microscope/camera/capture.py @@ -24,6 +24,7 @@ def clear_tmp(): for f in files: os.remove(f) logging.debug("Removed {}".format(f)) + logging.debug("Cleared {}.".format(TEMP_CAPTURE_PATH)) def capture_from_dict(capture_dict): diff --git a/openflexure_microscope/microscope.py b/openflexure_microscope/microscope.py index db84b0b3..c4e2efc0 100644 --- a/openflexure_microscope/microscope.py +++ b/openflexure_microscope/microscope.py @@ -73,10 +73,12 @@ class Microscope(object): def close(self): """Shut down the microscope hardware.""" + logging.info("Closing {}".format(self)) if self.camera: self.camera.close() if self.stage: self.stage.close() + logging.info("Closed {}".format(self)) def attach(self, camera: StreamingCamera, stage: OpenFlexureStage): """