first process of python popen pipe can't be killed

梦想的初衷 提交于 2019-12-11 14:44:07

问题


I am using this code

p1 = Popen(['rtmpdump'] + cmd_args.split(' '), stdout=PIPE)
p2 = Popen(player_cmd.split(' '), stdin=p1.stdout, stderr=PIPE)
p2.wait()
# try to kill rtmpdump
# FIXME: why is this not working ?
try:
    p2.stdin.close()
    p1.stdout.close()
    p1.kill()
except AttributeError:
    # if we use python 2.5
    from signal import SIGTERM, SIGKILL
    from os import kill
    kill(p1.pid, SIGKILL)

when p1 terminates then p2 is terminated too.

The problem is:

If I manually close p2 (it's mplayer), rtmpdump/p1 is still running. I tried various things like what is above but I still can't kill it. i tried with adding close_fds=True.

so may be rtmpdump still tries to write to stdout. but why is this cause kill() to fail ?

full source code: http://github.com/solsticedhiver/arte-7.py


回答1:


Here is the fix. call wait() after kill() to really kill the zombie process

# kill the zombie rtmpdump
try:
  p1.kill()
  p1.wait()
except AttributeError:
    # if we use python 2.5
    from signal import SIGKILL
    from os import kill, waitpid
    kill(p1.pid, SIGKILL)
    waitpid(p1.pid, 0)


来源:https://stackoverflow.com/questions/3472981/first-process-of-python-popen-pipe-cant-be-killed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!