I have read and tried 8 different methods answered on several questions about this. I opened a process in python and want to read its output even if the process hasn't terminated yet. The process doesn't complete usually for at least 1 minute or until an interrupt is sent. No matter what I try, I can't get it to read the output. I know the command and args I passed work because when I change it to subprocess.call(cmd, args), it prints everything to the screen. I also checked that the process is running with ps -ax. Here is an example of what I'm trying (cat /dev/random is UNRELATED to my project):
proc = subprocess.Popen(["cat", "/dev/random"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print("Process started.")
Here is what I have tried that has failed so far:
for line in iter(p.stdout.readline, ''):
strLine = str(line).rstrip()
print(">>> " + strLine )
sys.stdout.flush()
And
output, error = proc.communicate()
print output
And
while proc.poll() is None:
print("Still waiting.")
print(proc.stdout.readline(1))
There are more solutions I tried that are variations of this but no luck. When the call function is used without changing stdout, everything prints correctly to the console. What am I doing wrong?
I'm using Python 2.6.