0

Am able to read the python subprocess stdout using below method in real time,

import subprocess
command = "/tmp/testscript"
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
while True:
    output = proc.stdout.readline()
    print(output)
    if output.decode() == '' and proc.poll() is not None:
        break

But when I try to read both stdout and stderr in real time its not working as expected and getting the data printed on the console only when the subprocess execution completes.

import subprocess
command = "/tmp/test.sh"
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
while True:
    output = proc.stdout.readline()
    error = proc.stderr.readline()
    print(output)
    print(error)
    if output.decode() == '' and proc.poll() is not None:
        break

As far as what I understood is that in the while it just read the lines of stdout and then stderr, then printing both the variable values and moving to next loop, but not sure why its not working as expected. Any idea would be helpful.

Note : Am using python3.7

Karthi1234
  • 949
  • 1
  • 8
  • 28

0 Answers0