7stud wrote: > 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() > Let me ask you a question: what conditions have to be true to this statement to terminate?
A: the Python driver.py process has to close its output file. Since it doesn't do this (instead writing a little bit of output then going into an infinite loop) the whole thing just sits there consuming CPU time. > 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: > Who told you pipes should be unbuffered by default, and what difference does that make anyway? The reason it works now is that your program closes its standard output by default when it terminates. > 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. > Of course it does, for the reasons mentioned above. file.read() only returns when it has consumed *all* the data from the file (which means the write must close the file for the reader to be able to return). regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://del.icio.us/steve.holden --------------- Asciimercial ------------------ Get on the web: Blog, lens and tag the Internet Many services currently offer free registration ----------- Thank You for Reading ------------- -- http://mail.python.org/mailman/listinfo/python-list