2013-11-23 92 views
1

我正在使用守護進程模塊編寫python守護進程。我希望它響應SIGALRM,SIGHUP,SIGUSR1等來執行守護進程控制功能。python守護進程信號處理:無法覆蓋默認值

我發現信號處理程序被調用OK,但守護進程終止,當我期望它繼續運行。如何讓處理程序在不終止守護進程的情況下運行?

我試着用signal.signal和context.signal_map註冊處理程序。兩種情況下的行爲都是相同的。

原處理程序是這樣的:

def myhandler(signum, frame): 
    logger.info("Received Signal: %s at frame: %s" % (signum, frame)) 

信號註冊看起來像這樣

context.signal_map = { 
    signal.SIGHUP: myhandler, 
    signal.SIGUSR1: myhandler, 
    signal.SIGUSR2: myhandler, 
} 
+0

你能告訴你所有的代碼?這個問題應該是其他地方,因爲你的映射似乎是正確 – andref

回答

0

這段代碼工作對我來說:

import signal 
import daemon 
import time 


def do_main_program(): 
    while True: 
     with open("/tmp/current_time.txt", "w") as f: 
      f.write("The time is now " + time.ctime()) 
     time.sleep(5) 


def reload_program_config(signum, frame): 
    with open("/tmp/reload.txt", "w") as f: 
     f.write("The time is now " + time.ctime()) 
    return None 


def run(): 
    context = daemon.DaemonContext() 
    context.signal_map = { 
     signal.SIGHUP: 'terminate', 
     signal.SIGUSR1: reload_program_config, 
    } 
    with context: 
     do_main_program() 


if __name__ == "__main__": 
    run()