简体   繁体   中英

Python Daemon, repeat function every x seconds

I want to repeat a function every x seconds when the code its working like deamon in Python 2.6 for Linux. I have some code but its giving me a lot of problems. Is it possible to call a another file.py instead to write the code inside?

Here is the code:

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()

Sometimes it isn't worth working through the daemon-specific details. Have a look at supervisord , a process control system that makes it easy to wrap daemonic behaviors around existing applications.

What's wrong with:

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()

Take a look at the Fat Controller which can repeat any program every x seconds. It can also handle failures and offer strategies for parallel execution if needed and also the ability to run as a daemon.

There's documentation, examples and use cases at the official website:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM