Created
June 19, 2015 02:32
-
-
Save curzona/7fd37ae8506819583056 to your computer and use it in GitHub Desktop.
cleaning up child process tree spawned from test case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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