Python守护进程,每隔x秒重复一次

时间:2013-11-10 21:27:07

标签: python daemon

我希望每隔x秒重复一次函数,当代码在Python 2.6 for Linux中像deamon一样工作时。 我有一些代码,但它给了我很多问题。是否可以调用另一个file.py而不是在里面编写代码?

以下是代码:

import daemon   
import threading

def hello():
    print "hello, world"
    t = threading.Timer(2.0, hello).start()

def run():
    with daemon.DaemonContext():
        hello()

if __name__ == "__main__":
    run()

3 个答案:

答案 0 :(得分:3)

有时候通过守护进程特定的细节是不值得的。看看supervisord,这是一个过程控制系统,可以很容易地围绕现有应用程序包装守护进程。

答案 1 :(得分:2)

出了什么问题:

import daemon   
import threading
import another_file

def problematic_func_loop():
    another_file.peoblematic_func()
    t = threading.Timer(60.0, problematic_func_loop).start()

def run():
    with daemon.DaemonContext():
        problematic_func_loop()

if __name__ == "__main__":
    run()

答案 2 :(得分:0)

查看Fat Controller,它可以每隔x秒重复一次程序。它还可以处理故障并提供并行执行的策略(如果需要)以及作为守护程序运行的能力。

官方网站上有文档,示例和用例:

http://fat-controller.sourceforge.net/