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