【问题标题】:first process of python popen pipe can't be killedpython popen pipe的第一个进程不能被杀死
【发布时间】:2010-08-13 00:05:43
【问题描述】:

我正在使用此代码

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)

p1 终止时,p2 也将终止。

问题是:

如果我手动关闭 p2(它是 mplayer),rtmpdump/p1 仍在运行。 我尝试了各种类似上面的东西,但我仍然无法杀死它。 我尝试添加close_fds=True

所以可能是 rtmpdump 仍然尝试写入标准输出。但是为什么这会导致 kill() 失败?

完整源代码:http://github.com/solsticedhiver/arte-7.py

【问题讨论】:

  • 它根本不会杀死进程。但没有错误或异常。我只是使用 'pgrep rtmp' 来处理该进程,但实际上,它处于 状态。寻求有关已失效进程的帮助,我发现这些进程需要等待系统调用??偶然地,我尝试添加一个 p1.wait() 并且现在可以使用。即 rtmpdump 真的被杀死并且 try: p1.kill() p1.wait() wait before kill 不起作用。我不明白杀死后等待应该如何工作,但确实如此。
  • @solsTiCe:如果您回答了自己的问题而不是对其进行编辑,那就太好了。现在,其他人在搜索旧的、未回答的问题时,会一次又一次地找到您的问题。你能这样做吗?然后将该答案标记为已接受。

标签: python pipe kill popen zombie-process


【解决方案1】:

这里是修复。在kill()之后调用wait()来真正杀死僵尸进程

# 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)

【讨论】:

    猜你喜欢
    • 2010-10-07
    • 2014-03-28
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    相关资源
    最近更新 更多