Skip to content

Commit

Permalink
Made modifications to main.py in the adafruithat and planktoscopehat
Browse files Browse the repository at this point in the history
  • Loading branch information
Oumayma-hy committed Apr 29, 2024
1 parent 4bc7e92 commit e1c02a7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
20 changes: 15 additions & 5 deletions control/adafruithat/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,24 @@ def handler_stop_signals(signum, _):
logger.success("Looks like the controller is set up and running, have fun!")
planktoscope.light.ready()

# With the creation of this dictionary to keep track of running threads, we can easily
# update the code if we decide to add another process, such as for the pump.
running_threads = {
"stepper": stepper_thread,
"imager": imager_thread
}

while run:
# TODO look into ways of restarting the dead threads
logger.trace("Running around in circles while waiting for someone to die!")
if not stepper_thread.is_alive():
logger.error("The stepper process died unexpectedly! Oh no!")
break
if not imager_thread or not imager_thread.is_alive():
logger.error("The imager process died unexpectedly! Oh no!")
# Check if any threads have terminated unexpectedly and log the error without exiting
for thread_name, thread in running_threads.items():
if not thread or not thread.is_alive():
logger.error(f"The {thread_name} process terminated unexpectedly!")
del running_threads[thread_name] # Remove the dead thread from the dictionary
# Check if all threads have terminated so we can exit the program
if not running_threads: #checks if there is no running thread left
logger.error("All processes terminated unexpectedly! Exiting...")
break
time.sleep(1)

Expand Down
21 changes: 16 additions & 5 deletions control/planktoscopehat/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,25 @@ def handler_stop_signals(signum, frame):

logger.success("Looks like everything is set up and running, have fun!")


# With the creation of this dictionary to keep track of running threads, we can easily
# update the code if we decide to add another process, such as for the pump.
running_threads = {
"stepper": stepper_thread,
"imager": imager_thread
}

while run:
# TODO look into ways of restarting the dead threads
logger.trace("Running around in circles while waiting for someone to die!")
if not stepper_thread.is_alive():
logger.error("The stepper process died unexpectedly! Oh no!")
break
if not imager_thread or not imager_thread.is_alive():
logger.error("The imager process died unexpectedly! Oh no!")
# Check if any threads have terminated unexpectedly and log the error without exiting
for thread_name, thread in running_threads.items():
if not thread or not thread.is_alive():
logger.error(f"The {thread_name} process terminated unexpectedly!")
del running_threads[thread_name] # Remove the dead thread from the dictionary
# Check if all threads have terminated so we can exit the program
if not running_threads: #checks if there is no running thread left
logger.error("All processes terminated unexpectedly! Exiting...")
break
time.sleep(1)

Expand Down

0 comments on commit e1c02a7

Please sign in to comment.