Reputation: 3161
I'm launching a subprocess with Popen
, and I expect it to finish exit. However, not only does the process not exit, but sending sigkill to it still leaves it alive! Below is a script that demonstrates:
from subprocess import Popen
import os
import time
import signal
command = ["python","--version"]
process = Popen(command)
pid = process.pid
time.sleep(5) #ample time to finish
print pid
print "Sending sigkill"
os.kill(pid,signal.SIGKILL)
try:
#Kill with signal 0 just checks whether process exists
os.kill(pid,0)
print "Process still alive immediately after (not so bad...)!"
except Exception as e:
print "Succeeded in terminating child quickly!"
time.sleep(20) #Give it ample time to die
#Kill with signal 0 just checks whether process exists
try:
os.kill(pid,0)
print "Process still alive! That's bad!"
except Exception as e:
print "Succeeded in terminating child!"
For me, this prints:
77881
Python 2.7.10
Sending sigkill
Process still alive immediately after (not so bad...)!
Process still alive! That's bad!
Not only can this script verify that the child is still alive after it should have finished, but I can use ps
on the process id that's printed and see that it still exists. Oddly, ps lists the process name as (Python)
(note the parenthesis).
Upvotes: 1
Views: 1886
Reputation: 30151
You need to either call process.wait()
, or use signal.signal(signal.SIGCHLD, signal.SIG_IGN)
once to indicate that you don't intend to wait for any children. The former is portable; the latter only works on Unix (but is POSIX-standard). If you do neither of these things, on Unix the process will hang around as a zombie, and Windows has a similar behavior if you keep the process handle open.
Upvotes: 3