@@ -0,0 +1,695 @@
+import os%0Aimport sys%0Aimport signal%0Aimport psutil%0Aimport atexit%0Aimport contextlib%0A%0A%0Adef kill_children_processes():%0A    try:%0A        pid = os.getpid()%0A        p = psutil.Process(pid)%0A        for i in range(3):%0A            children = p.children(recursive=True)%0A            if len(children) == 0:%0A                break%0A            for process in children:%0A                process.send_signal(signal.SIGKILL)%0A        sys.exit()%0A    except:%0A        pass%0A%0A%0A@contextlib.contextmanager%0Adef cleanup_on_exit(sig=signal.SIGKILL):%0A    os.setpgrp()%0A    atexit.register(kill_children_processes)%0A    try:%0A        yield%0A    except (KeyboardInterrupt, SystemExit):%0A        os.killpg(os.getpgid(os.getpid()), sig)%0A

Refactor cleanup