Read from stdouton Popen on WinXP?

Adriaan Renting renting at astron.nl
Wed Aug 17 10:48:03 EDT 2005


As far as I understand it, the buffering is indeed happening in the .exe, depending on what it "thinks" it's stout is connected to. There's nothing the operating system, or Python can do about it, if the .exe was created using #include <stdio.h>.

This might help:
http://www.cplusplus.com/ref/cstdio/
http://htmlhelp.sunsite.dk/page.php/9/ch13s20.html
http://pexpect.sourceforge.net/

Especially read the FAQ of Pexpect, it's quite clear.

It took me some time to figure it out too, and I'm not realy happy about it, but I suppose that the advantage is, that it's all K&R compatible in this way.

I find that as long as one of the stdin/out/err are connected to a terminal, stdio will use them as character devices. This is why .exe | more works I think: more's stdout$err are a terminal, therefore it's std is a character device, and then the pipe is a character device, and then the .exe will have stdout as a character device. 
I do not know al the intricate details, but for me Pexpect works, and it's FAQ is quite clear, but AFAIK there are no pseudo-terminals on Windows, so you'll need to find a different trick.

Adriaan.
 
>>>Matthew Henry <mhenry1384 at gmail.com> 08/17/05 3:42 pm >>> 
On 8/17/05, Adriaan Renting <renting at astron.nl> wrote: 
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. 
 
Thanks for the info.  I am a longtime C++ programmer, but all GUI 
stuff so I'm afraid I don't really understand how the stdio stuff 
works. 
 
>From the cmd prompt, I can pipe the output of the .exe to "more" and 
see it work in realtime.  Thus I presume the buffering is happening on 
the Python end (or the stdio library that Python is using).  Not in 
the .exe. 
 
So, assuming that's true, why would it help to have the created 
process flush its buffer?  Does that put some kind of special 
character in the buffer that tells Python's stdio library to clear out 
its buffer? 
 
Or is my assumption incorrect, and the buffering is happening in the 
.exe and the cmd shell is working some magic to get at the output? 
 
Know of a good (online) reference that explains how stdin, stdout, 
stderr really works under the covers?  Especially under Windows? 




More information about the Python-list mailing list