@contextlib.contextmanager
def kill_on_error(proc):
 with proc:
  try:
   (yield proc)
  except:
   proc.kill()
   raise

@ contextlib . contextmanager def kill_on_error ( proc ) : with proc : try : ( yield proc ) except : proc . kill ( ) raise
context manager killing the subprocess if a python exception is raised .