0

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

2
  • It simply does not kill the process. But no error or exception. I was only using 'pgrep rtmp' to look after that process but in fact, it is in <defunct> state. Looking for help on defunct process, I found that wait system call is needed for those process ?? By chance, I tried to add a p1.wait() and this works now. i.e. rtmpdump is really killed and <defunct> try: p1.kill() p1.wait() wait before kill does not work. I don't understand how wait is supposed to work after kill but it is.
    – solsTiCe
    Commented Aug 13, 2010 at 11:55
  • @solsTiCe: It would have been nice if you had answered your own question instead of editing it. Now others find your question again and again, when searching for old, unanswered, questions. Can you do so? And then mark that answer as accepted.
    – Anthon
    Commented Apr 7, 2013 at 18:53

1 Answer 1

0

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)

Not the answer you're looking for? Browse other questions tagged or ask your own question.