Tries hard to terminate and ultimately kill all the children of this process.
(process, timeout=3)
| 96 | super().destroy() |
| 97 | |
| 98 | def reap(process, timeout=3): |
| 99 | "Tries hard to terminate and ultimately kill all the children of this process." |
| 100 | def on_terminate(proc): |
| 101 | pass |
| 102 | # print("process {} terminated with exit code {}".format(proc.pid, proc.returncode)) |
| 103 | |
| 104 | try: |
| 105 | procs = process.children(recursive=True) |
| 106 | # send SIGTERM |
| 107 | for p in procs: |
| 108 | p.terminate() |
| 109 | gone, alive = psutil.wait_procs(procs, timeout=timeout, callback=on_terminate) |
| 110 | if alive: |
| 111 | # send SIGKILL |
| 112 | for p in alive: |
| 113 | p.kill() |
| 114 | gone, alive = psutil.wait_procs(alive, timeout=timeout, callback=on_terminate) |
| 115 | if alive: |
| 116 | # give up |
| 117 | for p in alive: |
| 118 | print("process {} survived SIGKILL; giving up" % p.pid) |
| 119 | |
| 120 | process.kill() |
| 121 | except: |
| 122 | print("Killing failed; assuming process exited early.") |
| 123 | |
| 124 | def suspend(process): |
| 125 |