Python 2.0 - win32pipe routines inclusion for Windows

Niels Diepeveen niels at endea.demon.nl
Thu Jul 27 08:40:22 EDT 2000


David Bolen schreef:
...
> since the os.popen#() routines can standardize on
> using the first (or only for the original os.popen) file for returning
> the exit code on the close, and since that's always stdin of the child
> (except for the pair of os.popen() write modes, for which there's no
> connection to the child stdin) it minimizes the risk of any deadlock
> waiting for the child process during the close since the wait is
> performed as part of closing stdin to the child process.

I don't quite see how you can safely wait() for the child process before
you close *all* the pipes. Given that pipes have limited buffer space,
the child process might want to output more than that amount of data
after encountering EOF on the input. Consider this example:

#---
import popen2

def getstring():
    # get a string from somewhere

def usestring(s):
    # do something with s

stdin, stdout = popen2.popen2('sort')
while 1:
    s = getstring()
    if not s:
        break
    stdin.write('%s\n', s)
status = stdin.close() #XXX deadlock here
while 1:
    s = stdout.readline()
    if not s:
        break
    usestring(s)
stdout.close()
#---

I think this would, with a sufficient amount of data to be sorted, be
guaranteed to give a situation where the parent is waiting for the child
to terminate, while the child is waiting for the parent to read its
output.

-- 
Niels Diepeveen
Endea automatisering




More information about the Python-list mailing list