Last active
August 29, 2015 14:14
-
-
Save Zuckonit/3e2ab30dac4fbc000983 to your computer and use it in GitHub Desktop.
killer.py
This file contains 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
#https://blog.tonyseek.com/post/kill-the-descendants-of-subprocess/ | |
import signal | |
import os | |
import contextlib | |
import subprocess | |
import logging | |
import warnings | |
@contextlib.contextmanager | |
def process_fixture(shell_args): | |
proc = subprocess.Popen(shell_args, preexec_fn=os.setsid) | |
try: | |
yield | |
finally: | |
proc.terminate() | |
proc.wait() | |
try: | |
os.killpg(proc.pid, signal.SIGTERM) | |
except OSError as e: | |
warnings.warn(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment