python守護進程

1.守護進程python

  [1]使用runner這個模塊直接建立守護進程,很是方便。app

  [2]運行方法:python xxx.py start|stop|restartspa

  [3]調用python xxx.py stop時,會給進程發送signal.SIGTERM信號,因此能夠捕獲此信號進行程序退出前的處理。rest

  [4]流程:code

    1)調用python xxx.py start啓動一個守護進程,例如進程號是1000。blog

    2)調用python xxx.py stop時,會給進程1000發送一個signal.SIGTERM信號,進程1000結束。進程

# -*- coding: utf-8 -*-
import os,sys,time
import threading
import signal
from daemon import runner

class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/null'
        self.stderr_path = '/dev/null'
        self.pidfile_path = '/tmp/test.pid'
        self.pidfile_timeout = 5
        
    def run(self):
        #在這裏添加主流程

        while True:
            time.sleep(10)
    
if __name__ == "__main__":
    app = App()
    daemon_runner = runner.DaemonRunner(app)
    daemon_runner.do_action()  
相關文章
相關標籤/搜索