1

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()
alko
  • 46,136
  • 12
  • 94
  • 102
John Lapoya
  • 582
  • 1
  • 8
  • 21
  • 1
    Your question might have been answered here: http://stackoverflow.com/questions/2223157/how-to-execute-a-function-asynchronously-every-60-seconds-in-python – kiasy Nov 10 '13 at 21:29

3 Answers3

3

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.

Chris Johnson
  • 20,650
  • 6
  • 81
  • 80
2

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()
alko
  • 46,136
  • 12
  • 94
  • 102
  • i've tried the code above but i got `AttributeError: 'module' object has no attribute 'DaemonContext'` – Jacer Omri May 30 '14 at 19:49
  • i figured it out, it was a versioning issue, the daemon package in pypi was 1.5.5 which has DaemonContext, the version installed with pip command was 1.0, which hasn't. – Jacer Omri May 30 '14 at 20:18
0

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/

SlappyTheFish
  • 2,344
  • 3
  • 34
  • 41