Popen pipe hang

Jean-Paul Calderone exarkun at divmod.com
Mon May 12 22:35:08 EDT 2008


On Mon, 12 May 2008 17:35:44 -0700 (PDT), schickb <schickb at gmail.com> wrote:
>I'm trying to pipe data that starts life in an array('B') object
>through several processes. The code below is a simplified example. The
>data makes it through, but the wait() always hangs. Is there a better
>way to indicate src.stdin has reach EOF?
>
>from subprocess import Popen, PIPE
>from array import array
>
>arr = array('B')
>arr.fromstring("hello\n")
>
>src = Popen( ["cat"], stdin=PIPE, stdout=PIPE)
>dst = Popen( ["cat"], stdin=src.stdout)
>arr.tofile(src.stdin)
>src.stdin.close()
>dst.wait()

Alas, you haven't actually closed src's standard input.  Though you did
close src.stdin, you didn't close the copy of it which was inherited by
dst!  So though the file descriptor is no longer open in your main process,
it remains open due to the reference dst has to it.  You can fix this by
having Popen close all file descriptors except 0, 1, and 2 before it execs
cat - pass close_fds=True to the 2nd Popen call and you should get the
behavior you want.

Jean-Paul



More information about the Python-list mailing list