python - Why does a Popen child process not die? -
i'm launching subprocess popen
, , expect finish exit. however, not process not exit, sending sigkill still leaves alive! below script 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 finish print pid print "sending sigkill" os.kill(pid,signal.sigkill) try: #kill signal 0 checks whether process exists os.kill(pid,0) print "process still alive after (not bad...)!" except exception e: print "succeeded in terminating child quickly!" time.sleep(20) #give ample time die #kill signal 0 checks whether process exists try: os.kill(pid,0) print "process still alive! that's bad!" except exception e: print "succeeded in terminating child!"
for me, prints:
77881 python 2.7.10 sending sigkill process still alive after (not bad...)! process still alive! that's bad!
not can script verify child still alive after should have finished, can use ps
on process id that's printed , see still exists. oddly, ps lists process name (python)
(note parenthesis).
you need either call process.wait()
, or use signal.signal(signal.sigchld, signal.sig_ign)
once indicate don't intend wait children. former portable; latter works on unix (but posix-standard). if neither of these things, on unix process hang around zombie, , windows has similar behavior if keep process handle open.
Comments
Post a Comment