@@ -53,14 +53,11 @@ def __init__(self, exercise_command, gui_command, update_callback):
5353 self .gui_server .kill ()
5454 raise RuntimeError (f"Exercise GUI { gui_command } could not be run" )
5555
56- self .running = True
57-
58- self .start_send_freq_thread ()
5956
6057
6158 def send_freq (self , exercise_connection , is_alive ):
6259 """Send the frequency of the brain and gui to the exercise server"""
63- while is_alive () :
60+ while self . running :
6461 exercise_connection .send (
6562 """#freq{"brain": 20, "gui": 10, "rtf": 100}""" )
6663 time .sleep (1 )
@@ -71,9 +68,16 @@ def send_pick(self, data):
7168
7269 def start_send_freq_thread (self ):
7370 """Start a thread to send the frequency of the brain and gui to the exercise server"""
74- daemon = Thread (target = lambda : self .send_freq (self .exercise_connection ,
71+ self .running = True
72+ self .send_freq_thread = Thread (target = lambda : self .send_freq (self .exercise_connection ,
7573 lambda : self .is_alive ), daemon = False , name = 'Monitor frequencies' )
76- daemon .start ()
74+ self .send_freq_thread .start ()
75+
76+ def stop_send_freq_thread (self ):
77+ """Stop the thread sending the frequency of the brain and gui to the exercise server"""
78+ if self .running :
79+ self .running = False
80+ self .send_freq_thread .join ()
7781
7882 def _run_exercise_server (self , cmd , log_file , load_string , timeout : int = 5 ):
7983 process = subprocess .Popen (f"{ cmd } " , shell = True , stdout = sys .stdout , stderr = subprocess .STDOUT ,
@@ -131,7 +135,7 @@ def pause(self):
131135
132136 def restart (self ):
133137 # Terminate current processes
134- self .running = False
138+ self .stop_send_freq_thread ()
135139 home_dir = os .path .expanduser ('~' )
136140 stop_process_and_children (self .exercise_server )
137141 try :
@@ -142,7 +146,9 @@ def restart(self):
142146 process_ready ,self .exercise_server = self ._run_exercise_server (f"python { self .exercise_command } " ,
143147 f'{ home_dir } /ws_code.log' ,
144148 'websocket_code=ready' )
145- self .running = True
149+ if process_ready :
150+ self .start_send_freq_thread ()
151+
146152
147153
148154 @property
0 commit comments