Skip to content

Instantly share code, notes, and snippets.

@curzona
Created June 19, 2015 02:32
Show Gist options
  • Select an option

  • Save curzona/7fd37ae8506819583056 to your computer and use it in GitHub Desktop.

Select an option

Save curzona/7fd37ae8506819583056 to your computer and use it in GitHub Desktop.
cleaning up child process tree spawned from test case
import time
import subprocess
import psutil, os
def kill_proc_tree(pid, including_parent=True):
parent = psutil.Process(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 test_foo():
try:
p = subprocess.Popen(['./my-sleep.sh'])
p.wait()
except:
print "cleaning up"
#TODO: doesn't kill process tree
#p.kill()
kill_proc_tree(p.pid)
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment