@@ -27,7 +27,7 @@ def start_lsl():
27
27
try :
28
28
# Start the LSL stream as a subprocess
29
29
if sys .platform == "win32" :
30
- lsl_process = subprocess .Popen (["python" , "chords.py" , "--lsl" ],stdout = subprocess .PIPE ,stderr = subprocess .PIPE , creationflags = subprocess .CREATE_NO_WINDOW )
30
+ lsl_process = subprocess .Popen (["python" , "chords.py" , "--lsl" ], stdout = subprocess .PIPE , stderr = subprocess .PIPE , creationflags = subprocess .CREATE_NO_WINDOW )
31
31
else :
32
32
lsl_process = subprocess .Popen (["python" , "chords.py" , "--lsl" ],stdout = subprocess .PIPE ,stderr = subprocess .PIPE )
33
33
output = lsl_process .stderr .readline ().decode ().strip () # Read the initial stderr line
@@ -79,18 +79,36 @@ def app_status():
79
79
80
80
@app .route ("/stop_lsl" , methods = ['POST' ])
81
81
def stop_lsl ():
82
+ stop_all_processes ()
83
+ return jsonify ({'status' : 'LSL Stream and applications stopped and server is shutting down.' })
84
+
85
+ def stop_all_processes ():
86
+ global lsl_process , app_processes
87
+
82
88
# Terminate LSL process
83
89
if lsl_process and lsl_process .poll () is None :
84
90
lsl_process .terminate ()
91
+ lsl_process .wait (timeout = 3 )
92
+ if lsl_process .poll () is None :
93
+ lsl_process .kill ()
85
94
86
95
# Terminate all app processes
87
96
for app_name , process in app_processes .items ():
88
97
if process .poll () is None :
89
98
process .terminate ()
99
+ process .wait (timeout = 3 )
100
+ if process .poll () is None :
101
+ process .kill ()
90
102
91
- # Shutdown the server gracefully
92
- os ._exit (0 )
93
- return jsonify ({'status' : 'LSL Stream and applications stopped and server is shutting down.' })
103
+ app_processes .clear ()
104
+ print ("All processes terminated." )
105
+
106
+ def handle_sigint (signal_num , frame ):
107
+ print ("\n Ctrl+C pressed! Stopping all processes..." )
108
+ stop_all_processes ()
109
+ sys .exit (0 )
110
+
111
+ signal .signal (signal .SIGINT , handle_sigint ) # Register signal handler for Ctrl+C
94
112
95
113
if __name__ == "__main__" :
96
114
app .run (debug = True )
0 commit comments