I want to iterate over stdout of a subprocess while the subprocess is still running. My current approach looks like this:
proc = subprocess.Popen(['long_running_command'], stdout=subprocess.PIPE)
for line in iter(proc.stdout.readline, ''):
# do some stuff
print(line.rstrip())
At the moment the code in the for-loop will only be executed when the subprocess is finished. Is it possible to iterate over stdout of a runnnig subprocess and if yes how is it done? I know there are many existing threads with similar questions. However none of the provided answers seem to work for me.