How to process SIGTERM signal gracefully?

前端 未结 7 629
刺人心
刺人心 2020-11-22 16:02

Let\'s assume we have such a trivial daemon written in python:

def mainloop():
    while True:
        # 1. do
        # 2. some
        # 3. important
              


        
7条回答
  •  余生分开走
    2020-11-22 16:33

    Here is a simple example without threads or classes.

    import signal
    
    run = True
    
    def handler_stop_signals(signum, frame):
        global run
        run = False
    
    signal.signal(signal.SIGINT, handler_stop_signals)
    signal.signal(signal.SIGTERM, handler_stop_signals)
    
    while run:
        pass # do stuff including other IO stuff
    

提交回复
热议问题