I have some code that needs to run against several other systems that may hang or have problems not under my control. I would like to use Python's multiprocessing to spawn child processes to run independent of the main program and then when they hang or have problems terminate them, but I am not sure of the best way to go about this.
When terminate is called it does kill the child process, but then it becomes a defunct zombie that is not released until the process object is gone. The example code below where the loop never ends works to kill it and allow a respawn when called again, but does not seem like a good way of going about this (i.e. multiprocessing.Process() would be better in the \__init__()).
Anyone have a suggestion?
class Process(object): def __init__(self): self.thing = Thing() self.running_flag = multiprocessing.Value("i", 1) def run(self): self.process = multiprocessing.Process(target=self.thing.worker, args=(self.running_flag,)) self.process.start() print self.process.pid def pause_resume(self): self.running_flag.value = not self.running_flag.value def terminate(self): self.process.terminate() class Thing(object): def __init__(self): self.count = 1 def worker(self,running_flag): while True: if running_flag.value: self.do_work() def do_work(self): print "working {0} ...".format(self.count) self.count += 1 time.sleep(1)