windows console program stdout is buffered when using pipe redirection
Asked Answered
R

0

6

i have a long run server program(say, program A) which is written in QT/c++. the program is not so stable so i decide to write a python script to restart it if it crashes. the problem is that the program may started fail(if i gave it an in-use port), print the error and then just hang there without quitting, so i must monitor the stdout of the program and kill it on failed startup.

this is a piece of my final code (well, in fact this is ok, you can just ignore it):

self.subp = subprocess.Popen(
    r'.\A.exe -server %d' % portnum,
    stdout=subprocess.PIPE, bufsize=1)
for line in iter(self.subp.stdout.readline, ''):
    print(line, end='')

but i found i can't read anything from subprocess's stdout, the readline method is just blocking there, and if i kill the A process, the python script just exit without any output. at the beginning, i thought it's an issue of subprocess module, but after some test i found it's not. if i replace the A.exe command line with some other windows console program, ping -t for example, everything works correctly. so i thought it may be the A program's problem.

luckily, i have the source code of A, here is a piece dealing with output:

printf("Server is starting on port %u\n", Config.ServerPort);

if(server->listen())
    printf("Starting successfully\n");
else
    printf("Starting failed!\n");

after some search i add fflush(stdout); to the end of this piece of code, rebuild the program, and now it works

so my problem is that i still can't understand, what is wrong with the original A program code? without forced flush, it can correctly print those strings in a windows console, immediately after the program started. why the output is buffered when using pipe on it's output? i read that in standard c implement, output will be flushed automatic on newline, but why not in my situation? is this a windows issue, or compiler issue?

the A program is compiled with QT/C++, QT version is 4.7.4(x32), the C++ compiler is the ming32 g++ come with QT(GCC 4.4.0), all tests were did on win7x64 platform, and my python version is 2.7.2

Roulers answered 27/1, 2012 at 17:4 Comment(2)
I came across a same question as yours. (#22527510) stdout of pipe redirect on windows is fully buffered defaultly, it means you cant read anything from pipe unless the subprocess run to end or invoke fflush after printf at subprocess. This is not windows issue, but by design, and its like a issue sometimes. And i havnt found a good solution for this question, tell me if u solve it.Silberman
i think the related link in comments in your link explains much, thanksRoulers

© 2022 - 2024 — McMap. All rights reserved.