@@ -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,38 @@ 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
+ try :
92
+ lsl_process .wait (timeout = 3 )
93
+ except subprocess .TimeoutExpired :
94
+ lsl_process .kill ()
85
95
86
96
# Terminate all app processes
87
97
for app_name , process in app_processes .items ():
88
98
if process .poll () is None :
89
99
process .terminate ()
100
+ try :
101
+ process .wait (timeout = 3 )
102
+ except subprocess .TimeoutExpired :
103
+ process .kill ()
90
104
91
- # Shutdown the server gracefully
92
- os ._exit (0 )
93
- return jsonify ({'status' : 'LSL Stream and applications stopped and server is shutting down.' })
105
+ app_processes .clear ()
106
+ print ("All processes terminated." )
107
+
108
+ def handle_sigint (signal_num , frame ):
109
+ print ("\n Ctrl+C pressed! Stopping all processes..." )
110
+ stop_all_processes ()
111
+ sys .exit (0 )
112
+
113
+ signal .signal (signal .SIGINT , handle_sigint ) # Register signal handler for Ctrl+C
94
114
95
115
if __name__ == "__main__" :
96
116
app .run (debug = True )
0 commit comments