From 1cf987439156d24956e398ea5293aff38da4f1ed Mon Sep 17 00:00:00 2001 From: Romain Bazile Date: Mon, 6 Sep 2021 18:58:23 +0200 Subject: [PATCH] better manage a segmenter failure to start and kill the process --- scripts/main.py | 28 +++++++++++++++------- scripts/planktoscope/segmenter/__init__.py | 9 ++++++- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index d0cb1a0..b78160f 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -66,6 +66,7 @@ run = True def handler_stop_signals(signum, frame): """This handler simply stop the forever running loop in __main__""" global run + logger.info(f"Received a signal asking to stop {signum}") run = False @@ -131,10 +132,15 @@ if __name__ == "__main__": # Starts the segmenter process logger.info("Starting the segmenter control process (step 4/4)") - segmenter_thread = planktoscope.segmenter.SegmenterProcess( - shutdown_event, "/home/pi/data" - ) - segmenter_thread.start() + try: + segmenter_thread = planktoscope.segmenter.SegmenterProcess( + shutdown_event, "/home/pi/data" + ) + except Exception as e: + logger.error("The segmenter control process could not be started") + else: + segmenter_thread = None + segmenter_thread.start() # Starts the module process # Uncomment here as needed @@ -154,28 +160,34 @@ if __name__ == "__main__": if not stepper_thread.is_alive(): logger.error("The stepper process died unexpectedly! Oh no!") break - if imager_thread and not imager_thread.is_alive(): + if not imager_thread or not imager_thread.is_alive(): logger.error("The imager process died unexpectedly! Oh no!") break - if not segmenter_thread.is_alive(): + if not segmenter_thread or not segmenter_thread.is_alive(): logger.error("The segmenter process died unexpectedly! Oh no!") break time.sleep(1) + display.display_text("Bye Bye!") logger.info("Shutting down the shop") shutdown_event.set() time.sleep(1) + stepper_thread.join() if imager_thread: imager_thread.join() - segmenter_thread.join() + if segmenter_thread: + segmenter_thread.join() # Uncomment this for clean shutdown # module_thread.join() + stepper_thread.close() if imager_thread: imager_thread.close() - segmenter_thread.close() + if segmenter_thread: + segmenter_thread.close() # Uncomment this for clean shutdown # module_thread.close() + display.stop() logger.info("Bye") diff --git a/scripts/planktoscope/segmenter/__init__.py b/scripts/planktoscope/segmenter/__init__.py index ec095d4..4ad6673 100644 --- a/scripts/planktoscope/segmenter/__init__.py +++ b/scripts/planktoscope/segmenter/__init__.py @@ -954,7 +954,14 @@ class SegmenterProcess(multiprocessing.Process): handler = functools.partial( planktoscope.segmenter.streamer.StreamingHandler, refresh_delay ) - server = planktoscope.segmenter.streamer.StreamingServer(address, handler) + try: + server = planktoscope.segmenter.streamer.StreamingServer(address, handler) + except Exception as e: + logger.exception( + f"An exception has occured when starting up the segmenter: {e}" + ) + raise e + self.streaming_thread = threading.Thread( target=server.serve_forever, daemon=True )