Clueless: piping between 2 non-python processes

Michael Lehmeier m_lehmeier at gmx.de
Sun Oct 26 04:51:36 EST 2003


On 2003-10-26, Donn Cave <donn at drizzle.com> wrote:
> Quoth Andrew Bennetts <andrew-pythonlist at puzzling.org>:
>
>|     # Wait for B to terminate
>|     os.waitpid(pidB, 0)
>
> OK, this is where the fun starts.  I understood the problem to be
> that B may fail to exit when A exits, so I think we really want
> to wait for A.  Or if the converse may also happen, then we need
> to wait for either and then dispatch the other.  In any case I think
> this is not too hard to figure out.  The most useful trick here is
> the os.WNOHANG flag to waitpid, which will allow one or more waits
> without blocking to see if B is really going to exit on its own.

This is what I have here now:

# Wait for B to terminate
os.waitpid(pidB, os.WNOHANG)

# Kill A (even if it has finished already... this might need a
# try/except)
print "Terminating A"
os.kill(pidA, signal.SIGKILL)

# Wait for A (to avoid zombies)
os.waitpid(pidA, os.WNOHANG)

print "Terminating B"
os.kill(pidA, signal.SIGKILL)

waitpid doesn't wait for anything here.
The os.kill are called immediately after start and ends the script.

If I understand it correctly, since B or A seem to take some time
starting up, waitpid ignores them because of WNOHANG.
That makes this waitpid pretty useless, doesn't it?
And even if, if A terminates sooner than B, wouldn't the script hang
then?

What about the Popen3 approach?
Wouldn't it be cleaner (once I get it running)?

Thanks so far.




More information about the Python-list mailing list