I am trying to write a python program to test a server written in C. The python program launches the compiled server using the subprocess
module:
pid = subprocess.Popen(args.server_file_path).pid
This works fine, however if the python program terminates unexpectedly due to an error, the spawned process is left running. I need a way to ensure that if the python program exits unexpectedly, the server process is killed as well.
Some more details:
- Linux or OSX operating systems only
- Server code can not be modified in any way
I would
atexit.register
a function to terminate the process:Or maybe:
Note that this doesn't help you if you do something nasty to cause python to die in a non-graceful way (e.g. via
os._exit
or if you cause aSegmentationFault
orBusError
)