if the written Python program is multithreaded or multi-process, it involves the issue of program security exit.
one, multi-process program exitthe child process needs to be killed one after the other before exiting the main process, or it will cause the main process to exit (CTRL + C) After the child process does not exit and continue to run.
?
Ii. exit of multi-threadedYou need to set the thread's "Daemon" property to "True" (The default is "False") when multiple threads are started .
otherwise the program will deadlock and cannot exit (CTRL + C);
Here is the sample code:
Import Signal
Import Threading
def Sig_handler (SIG, frame):
Try:
th_timeout.stop ()
Th_timeout.join ()
except Exception, ex:
exit (0)
?
def main ():
...
# #set Signal Handler
signal.signal (signal. SIGTERM, Sig_handler)
signal.signal (signal. SIGINT, Sig_handler)
Global Th_timeout
th_timeout = Threading. Thread (Target=your_application, args= ())
Th_timeout.setdaemon (' True ')
Th_timeout.start ()
if __name__ = = "__main__":
Main ()
?
Signal processing and program exit for Python