I'm trying to extract output from a running Python subprocess. For simplicity, I created a C executable that just prints out 0, 1, 2, etc. every second and runs indefinitely.
The Python script I have, along with a few other variations of a similar idea, all seem to do nothing for 5 minutes and then print a chunk of 300 lines to the terminal all at once. Instead, I am trying to have it print one line every second. Using Python 3.5.
import subprocess
import os
import sys
from time import sleep
def start_program():
args = ['./test_program']
p = subprocess.Popen(args, stdout=subprocess.PIPE, universal_newlines=True, bufsize=1)
return p
def print_output(p):
for line in p.stdout:
print(line, end='')
def main():
p = start_program()
print_output(p)
main()