【问题标题】:Python Daemon, repeat function every x seconds [duplicate]Python守护进程,每x秒重复一次函数
【发布时间】:2013-11-22 14:35:53
【问题描述】:

当代码在 Python 2.6 for Linux 中像守护进程一样工作时,我想每隔 x 秒重复一次函数。 我有一些代码,但它给我带来了很多问题。是否可以调用另一个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()

【问题讨论】:

标签: python daemon


【解决方案1】:

有时不值得仔细研究特定于守护进程的细节。看看supervisord,这是一个流程控制系统,可以轻松地将守护进程行为封装在现有应用程序周围。

【讨论】:

    【解决方案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()
    

    【讨论】:

    • 我已经尝试了上面的代码,但我得到了AttributeError: 'module' object has no attribute 'DaemonContext'
    • 我想通了,是版本问题,pypi中的daemon包是1.5.5,有DaemonContext,用pip命令安装的版本是1.0,没有。
    【解决方案3】:

    看看Fat Controller,它可以每隔 x 秒重复任何程序。如果需要,它还可以处理故障并提供并行执行策略,还可以作为守护进程运行。

    官网有文档、示例和用例:

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

    【讨论】:

      猜你喜欢
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-02
      • 1970-01-01
      • 2015-05-07
      相关资源
      最近更新 更多