如何优雅地退出python程序

时间:2023-01-09 04:06:31

如何优雅地退出python程序

一个单模的python程序,启动之后要能够优雅地关闭。即当用户按Ctrl+C或者kill pid的时候,程序都能从容关闭。实现起来非常简单。

is_running = True

def onSigChld(signo, frame):
    global is_running
    pid, status = os.waitpid(-1, os.WNOHANG)
    if pid:
      is_running = False
    pass

def onSigInt(signo, frame):
    global is_running
    is_running = False
    pass

def onSigTerm(signo, frame):
    global is_running
    is_running = False
    pass

def test():
    global is_running
    while is_running:
        time.sleep(1)
        print "."
    print "App exit gracefully."
    sys.exit(0)
    pass

if __name__ == "__main__":
    # 子进程退出后向父进程发送的信号
    signal.signal(signal.SIGCHLD, onSigChld)

    # 主进程退出信号
    signal.signal(signal.SIGINT, onSigInt)
    signal.signal(signal.SIGTERM, onSigTerm)

    test()