0

I have a program which starts from a python script called daemon.py, and this daemon script starts up 4 threads that are alive the whole time the program is running.

I want to use the initial daemon program as a watchdog for the other threads. I'm thinking that each thread will have its own time in the sqlite database and the daemon will count it down. Its the threads job to reset its own timer. If any of the timers make it to 0, the daemon will restart everything.

Is this a good way of doing this? if not how should I go about it?

Op De Cirkel
  • 28,647
  • 6
  • 40
  • 53
user185955
  • 359
  • 2
  • 8

1 Answers1

1

Your approach seems valid at a high level although without some more specifics as to the function of the 4 threads it is hard to say with an certainty. The main thread can provide guidance to the 4 threads by modifying instance variables of the threads. You do not need to use a database as a communication mechanism. Python has some useful thread features that might solve much of this for you.

Also, it sounds like you want the threads to timeout or die off after some amount of time and then get recreated. To accomplish this you can have the each of the 4 threads reset an instance variable like last_active and the main thread can use that information to act on the threads accordingly. There is no simple way to kill an arbitrary thread in python. My preferred approach is like this:

def run():
    while not self.kill_requested:
        ...
Jesse Harris
  • 1,131
  • 6
  • 10
  • The threads that are started should never die, the watchdog function is there just in case something goes wrong and a thread crashes or hangs. What python thread features are you talking about, queues? – user185955 Jul 01 '12 at 12:44
  • The watchdog can check the 4 threads on a regular interval to see if there are still running. If one has crashed, the watchdog can restart it. If a thread hangs it will be much tougher. Unlock processes which can be killed, in python there is no way to send a kill signal to the thread. Queues are one mechanism but there is the Event and Timer objects. I would make sure that my threads have timeouts on all IO operations(like HTTP callouts or database calls). A thread might keep track of the number of recent timeouts and the watch dog can query this and determine if the thread needs restarted. – Jesse Harris Jul 02 '12 at 02:27