I have to lose sight of something terrible. I need to execute a C program, display its output in real time, and finally parse its last line, which should be simple, since the last line printed is always the same.
process = subprocess.Popen(args, shell = True,
stdout = subprocess.PIPE, stderr = subprocess.PIPE)
while process.poll() is None:
out = process.stdout.readline()
if out:
last_non_empty_line = out
if verbose:
sys.stdout.write(out)
sys.stdout.flush()
From time to time, however, the last line is not printed. The default value for popens bufsize is 0, so it should be unbuffered. I also tried, to no avail, to add fflush (stdout) to the C code just before exiting, but it seems like there is absolutely no need to clear the stream before exiting the program.
Anyone ideas?
source
share