I need to stop \ kill all processes when there is an error \ exception. I found in the StackOwerflow solution to kill all processes using psutil , but from time to time I have a problem: when psutil kills the child and main processes, new processes may start and the code continues to run.
import psutil class MyClass: parent_pid = 0 ids_list = range(300) def main(self): self.parent_pid = os.getpid() pool = multiprocessing.Pool(3) for osm_id in self.ids_list: pool.apply_async(self.handle_country_or_region, kwds=dict(country_id=osm_id), error_callback=self.kill_proc_tree) pool.close() pool.join() def kill_proc_tree(self, including_parent=True): parent = psutil.Process(self.parent_pid) children = parent.children(recursive=True) for child in children: child.kill() psutil.wait_procs(children, timeout=5) if including_parent: parent.kill() parent.wait(5) def handle_country_or_region(self, country_id=None, queue=None): pass
It seems I need to stop the pool, not kill processes, but in this case, if I do
pool.close() pool.terminate() pool.join()
my terminal stops doing anything, the new line is completely empty (that is, without "â>"), and nothing happens.
Ideally, I want to have the following stream: if there is any error \ exception, stop \ kill all executions of the code and return to the interactive help in the terminal.
Can someone help me make it work correctly? I am using Python 3.5 and Ubuntu 15.10
source share