subprocess -- broken pipe error

7stud bbxx789_05ss at yahoo.com
Mon Jul 2 15:16:22 EDT 2007


Why doesn't the following program write to the file?

driver.py
-------
import subprocess as sub

p = sub.Popen(["python", "-u", "test1.py"], stdin=sub.PIPE,
stdout=sub.PIPE)


p.stdin.write("text3")

while True:
    pass
-------

test1.py:
---------
import sys

data = sys.stdin.read()

f = open("aaa.txt", "w")
f.write(data + "\n")
f.close()
-----------


After I hit Ctrl+C to end the program and look in the file, the text
wasn't written to the file.  But, if I change driver.py to the
following it works:

driver.py:
----------
import subprocess as sub

p = sub.Popen(["python", "-u", "test1.py"], stdin=sub.PIPE,
stdout=sub.PIPE)


p.stdin.write("text3")
-------

Ok. So that looks like the data is caught in a buffer--even though the
pipes should be unbuffered by default.  But this doesn't work:

driver.py
----------
import subprocess as sub

p = sub.Popen(["python", "-u", "test1.py"], stdin=sub.PIPE,
stdout=sub.PIPE)

p.stdin.write("text4")
p.stdin.flush()

while True:
    pass
-------

It just hangs, and then when I hit Ctrl+C and look in the file, the
data isn't in there.







More information about the Python-list mailing list