Read from stdouton Popen on WinXP?

Adriaan Renting renting at astron.nl
Wed Aug 17 03:05:29 EDT 2005


The only way I know how to do this is using a pseudo terminal (pty), but I think that only works on Unix. The problem is the way stdout in C works:
- If connected to a character device, it will flush after each line.
- If connected to a block device it will flush only after it's buffer is full. Any kind of pipe or file is a block device.

As this is how stdout works even your OS can't help, because as long as the application doesn't flush, the output doesn't even arrive at the pipe or file. If you have access to the source of the application you can force it to flush, otherwise you'll need to find out if anything similar to pty exists in Windows. I have 3 years of Windows programming experience, but I don't know of any solution.

This probably also means your solution of piping the .exe will not work.

Adriaan Renting.

>>> "mhenry1384" <mhenry1384 at gmail.com> 08/16/05 11:48 PM >>>
I am trying to run a program and filter the output on Windows XP.
Since I want to filter the output, I'd like to read it a line at a time
and only print the lines I care about.

p = subprocess.Popen(["doxygen.exe", r"Doxyfile.cfg"],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while 1:
 line = p.stdout.readline()
 if not line:
   break
 print line


The problem is that readline() only returns after the whole process has
completed.  I have tried various permutations such as using os.read()
and changing the bufsize parameter and using popen4.  To no avail.

Obviously, it should be possible to read stdout before the process
completes, since if I leave off the "stdout=" parameter, the full
output shows up in stdout in "realtime" as you'd expect.

About the only thing I can come up with is to pipe the .exe to another
python script which could communicate to the main script via TCP/IP,
but that seems ridiculous.  I searched the newsgroup and didn't see
anything particularly helpful.

Anyone have a non-ridiculous solution?

-- 
http://mail.python.org/mailman/listinfo/python-list



Adriaan Renting        | Email: renting at astron.nl
ASTRON                 | Phone: +31 521 595 217
P.O. Box 2             | GSM:   +31 6 24 25 17 28
NL-7990 AA Dwingeloo   | FAX:   +31 521 597 332
The Netherlands        | Web: http://www.astron.nl/~renting/



More information about the Python-list mailing list