I have a web server that needs to manage a separate multi-process subprocess (i.e. starting it and killing it).
For Unix-based systems, the following works fine:
# save the pid as `pid`
ps = subprocess.Popen(cmd, preexec_fn=os.setsid)
# elsewhere:
os.killpg(os.getpgid(pid), signal.SIGTERM)
I'm doing it this way (with os.setsid
) because otherwise killing the progress group will also kill the web server.
On Windows these os
functions are not available -- so if I wanted to accomplish something similar on Windows, how would I do this?
I'm using Python 3.5.
THIS ANSWER IS PROVIDED BY eryksun IN COMMENT. I PUT IT HERE TO HIGHLIGHT IT FOR SOMEONE MAY ALSO GET INVOLVED IN THIS PROBLEM。
Here is what he said:
I tried it with this and succeed: