A context manager for a subprocess that guarantees that a process is terminated, even if exceptions are thrown while using it.
The process_name argument is used for logging when the process goes down fighting. The process argument is a process returned by subprocess.Popen.
process = subprocess.Popen([‘patch’, ‘-o’, newfile, oldfile])
- with controlled_subprocess(“patch”, process) as p:
- # … do things with the process p
Once outside the with block, you can rest assured that the subprocess is no longer running.