私はひどく明白な何かを見落としているに違いありません。Cプログラムを実行し、その出力をリアルタイムで表示し、最後に最後の行を解析する必要があります。これは、印刷される最後の行が常に同じであるため、簡単なはずです。
process = subprocess.Popen(args, shell = True,
stdout = subprocess.PIPE, stderr = subprocess.PIPE)
# None indicates that the process hasn't terminated yet.
while process.poll() is None:
# Always save the last non-emtpy line that was output by the child
# process, as it will write an empty line when closing its stdout.
out = process.stdout.readline()
if out:
last_non_empty_line = out
if verbose:
sys.stdout.write(out)
sys.stdout.flush()
# Parse 'out' here...
ただし、最後の行が印刷されない場合があります。Popensのbufsizeのデフォルト値は0であるため、バッファリングされていないことになっています。また、終了する直前にCコードにfflush(stdout)を追加しようとしましたが、プログラムを終了する前にストリームをフラッシュする必要はまったくないようです。
アイデアは誰ですか?