Skip to content

Instantly share code, notes, and snippets.

@Zuckonit
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zuckonit/3e2ab30dac4fbc000983 to your computer and use it in GitHub Desktop.
Save Zuckonit/3e2ab30dac4fbc000983 to your computer and use it in GitHub Desktop.
killer.py
#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