I have a long server program (say program A) that is written in QT / C ++. the program is not so stable, so I decided to write a python script to reload it if it works. the problem is that the program may start to crash (if I gave it a port to use), print the error, and then just hang it without leaving it, so I have to keep track of the release of the program and kill it if it fails to start.
this is part of my last code (well, actually this is normal, 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 that I canβt read anything from the stdout subprocess, the readline method just blocks it, and if I kill process A, the python script will just exit without any output. at the beginning, I thought it was a subprocess module problem, but after some test I did not find it. if I replace the A.exe command line with some other Windows console program, for example ping -t, everything works correctly. so I thought it might be a problem with the program.
Fortunately, I have the source code A, here is the part regarding 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 searching add fflush(stdout);to the end of this piece of code, rebuild the program, and now it works
, , ? , Windows . ? , c- , ? Windows ?
QT/++, QT - 4.7.4 (x32), ++ - ming32 g++, QT (GCC 4.4.0), win7x64, python - 2.7.2