问题
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
来源:https://stackoverflow.com/questions/9037177/windows-console-program-stdout-is-buffered-when-using-pipe-redirection