the secret life of zombies

jsnx jason.dusek at gmail.com
Thu Oct 11 21:37:15 EDT 2007


I'd like a program, call it 'pipe-fitter', that connects and
interactive app
to three named pipes. The program will filter out EOF from the input
pipe, to
keep the interactive program alive for multiple commands. When the
interactive
app dies, by receiving 'exit', say, then we are done with it -- pipe-
fitter
bails out.

I've tried writing pipe-fitter in the shell (infinite loop), C (could
not get
off the ground at all with this one) and now Python. After trying a
few
different approaches, I was turned on to the subprocess module by
someone on
IRC. My program is almost right, except for damn zombies. Here is pipe-
fitter:

    https://svn.j-s-n.org/public/i-can-has-root/python/pipe-fitter.py

Say we make some fifos in a dir 'pipes'

  mkdir pipes
  mkfifo pipes/i pipes/o pipes/e

Now we call pipe-fitter thusly:

  pipe-fitter.py -d pipes sh -xv

This runs `sh -xv` with stdin, stdout and stderr connected to pipes/i,
pipes/o
and pipes/e respectively. We open three new terminals. In the first
one, we
put:

  echo "ls -l" > pipes/i

and in the second:

  cat pipes/o

and in the third:

  cat pipes/e

Once we enter the final command, the 'echo' returns and the text comes
streaming out in the 2nd and 3rd terminals. Wonderful. Now, let's try
`exit`.

  echo 'exit' > pipes/i

As expected, cat will terminate in the 2nd and 3rd terminals, with the
3rd
terminal displayin:

  + exit
  exit

However, pipe-fitter.py is still running. Why is that? It did not
'poll' at
the right time, it seems. If I try stuffing more input into pipes/i, I
get

  IOError: [Errno 32] Broken pipe

and pipe-fitter.py finally quits. Not exactly what I was hoping for.

Prowling around with PS, I find that the `sh -xv` called from within
pipe-fitter is a zombie for a spell; if pipe-fitter fails to catch it
at the
right time, there is no avoiding the IOError. I'm using
subprocess.Popen.poll() in a loop, to catch the process's termination
-- is
there something else I should be doing?




More information about the Python-list mailing list