python popen管道的第一个进程不能被杀死 [英] first process of python popen pipe can't be killed

查看:189
本文介绍了python popen管道的第一个进程不能被杀死的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码

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.

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.

因此rtmpdump可能仍尝试写入stdout.但是为什么这会导致kill()失败?

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

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

推荐答案

这里是解决方法.在kill()之后调用wait()真正杀死僵尸进程

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)

这篇关于python popen管道的第一个进程不能被杀死的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆