I am trying to design a python script to control a command line simulator program written in C, including parsing output values printed to the screen, and giving input commands.
So far I am able to capture the initial boot up output from the simulator, but it hangs at the end.
Basically how I have it set up, is I set up the subprocess:
proc = Popen('./sim1',stdin=PIPE,stdout=PIPE)
then I have a for loop to process the output and display it to the screen (this is for testing purposes, I will be taking specific values from the text output later)
for line in iter(proce.stdout.readline,''):
print(line.rstrip())
This hangs after all the text is printed, even though I added fflush(stdout) lines to the simulator below the printf statements (suggested here: catching stdout in realtime from subprocess)
I have added a exception that will break the loop if it detects a keyword on the last line, but it is not a very elegant solution that I would like to avoid.
After this loop I have code that inputs commands to the simulator, which I can tell work because if I remove the code to read the output from the simulator, the exit commands I send do exit the simulator.
It seems most of the examples I have seen are where people start the subprocess, capture output, and display it after the process exits. I need to capture output, display the output, and then give new commands, and display the next output, all without exiting the subprocess.
Is there something fundamental here that is causing my stdout issues?