@@ -87,7 +87,7 @@ def __init__(self):
87
87
self .mav_error = 0
88
88
self .altitude = 0
89
89
self .last_distance_announce = 0.0
90
- self .exit = False
90
+ self .stop_event = threading . Event ()
91
91
self .flightmode = 'MAV'
92
92
self .last_mode_announce = 0
93
93
self .last_mode_announced = 'MAV'
@@ -807,7 +807,7 @@ def process_stdin(line):
807
807
print ("%-15s : %s" % (cmd , help ))
808
808
return
809
809
if cmd == 'exit' and mpstate .settings .requireexit :
810
- mpstate .status .exit = True
810
+ mpstate .status .stop_event . set ()
811
811
return
812
812
813
813
if cmd not in command_map :
@@ -936,8 +936,12 @@ def mkdir_p(dir):
936
936
937
937
def log_writer ():
938
938
'''log writing thread'''
939
- while not mpstate .status .exit :
940
- mpstate .logfile_raw .write (bytearray (mpstate .logqueue_raw .get ()))
939
+ while not mpstate .status .stop_event .is_set ():
940
+ if not mpstate .logqueue_raw .empty ():
941
+ bytes = mpstate .logqueue_raw .get (block = False )
942
+ mpstate .logfile_raw .write (bytearray (bytes ))
943
+
944
+ # TODO consider wait() the stop event instead
941
945
timeout = time .time () + 10
942
946
while not mpstate .logqueue_raw .empty () and time .time () < timeout :
943
947
mpstate .logfile_raw .write (mpstate .logqueue_raw .get ())
@@ -1009,18 +1013,17 @@ def open_telemetry_logs(logpath_telem, logpath_telem_raw):
1009
1013
stat = os .statvfs (logpath_telem )
1010
1014
if stat .f_bfree * stat .f_bsize < 209715200 :
1011
1015
print ("ERROR: Not enough free disk space for logfile" )
1012
- mpstate .status .exit = True
1016
+ mpstate .status .stop_event . set ()
1013
1017
return
1014
1018
1015
1019
# use a separate thread for writing to the logfile to prevent
1016
1020
# delays during disk writes (important as delays can be long if camera
1017
1021
# app is running)
1018
1022
t = threading .Thread (target = log_writer , name = 'log_writer' )
1019
- t .daemon = True
1020
1023
t .start ()
1021
1024
except Exception as e :
1022
1025
print ("ERROR: opening log file for writing: %s" % e )
1023
- mpstate .status .exit = True
1026
+ mpstate .status .stop_event . set ()
1024
1027
return
1025
1028
1026
1029
@@ -1121,7 +1124,7 @@ def main_loop():
1121
1124
set_stream_rates ()
1122
1125
1123
1126
while True :
1124
- if mpstate is None or mpstate .status .exit :
1127
+ if mpstate is None or mpstate .status .stop_event . is_set () :
1125
1128
return
1126
1129
1127
1130
# enable or disable screensaver:
@@ -1218,12 +1221,12 @@ def main_loop():
1218
1221
1219
1222
def input_loop ():
1220
1223
'''wait for user input'''
1221
- while mpstate .status .exit is not True :
1224
+ while not mpstate .status .stop_event . is_set () :
1222
1225
try :
1223
1226
line = mpstate .rl .input ()
1224
1227
mpstate .input_queue .put (line )
1225
1228
except (EOFError , IOError ):
1226
- mpstate .status .exit = True
1229
+ mpstate .status .stop_event . set ()
1227
1230
1228
1231
1229
1232
def run_script (scriptfile ):
@@ -1407,7 +1410,6 @@ def run_startup_scripts():
1407
1410
1408
1411
# global mavproxy state
1409
1412
mpstate = MPState ()
1410
- mpstate .status .exit = False
1411
1413
mpstate .command_map = command_map
1412
1414
mpstate .continue_mode = opts .continue_mode
1413
1415
# queues for logging
@@ -1447,11 +1449,11 @@ def run_startup_scripts():
1447
1449
1448
1450
def quit_handler (signum = None , frame = None ):
1449
1451
# print('Signal handler called with signal', signum)
1450
- if mpstate .status .exit :
1452
+ if mpstate .status .stop_event . is_set () :
1451
1453
print ('Clean shutdown impossible, forcing an exit' )
1452
1454
sys .exit (0 )
1453
1455
else :
1454
- mpstate .status .exit = True
1456
+ mpstate .status .stop_event . set ()
1455
1457
1456
1458
# Listen for kill signals to cleanly shutdown modules
1457
1459
fatalsignals = [signal .SIGTERM ]
@@ -1584,7 +1586,7 @@ def quit_handler(signum=None, frame=None):
1584
1586
1585
1587
# use main program for input. This ensures the terminal cleans
1586
1588
# up on exit
1587
- while ( mpstate .status .exit is not True ):
1589
+ while not mpstate .status .stop_event . is_set ( ):
1588
1590
try :
1589
1591
if opts .daemon or opts .non_interactive :
1590
1592
time .sleep (0.1 )
@@ -1606,7 +1608,7 @@ def quit_handler(signum=None, frame=None):
1606
1608
m .init (mpstate )
1607
1609
1608
1610
else :
1609
- mpstate .status .exit = True
1611
+ mpstate .status .stop_event . set ()
1610
1612
sys .exit (1 )
1611
1613
1612
1614
if opts .profile :
0 commit comments