0

When I use subprocess.run with capture_output=True, I notice that the output is not in real time. As example, the following script will wait till ping is completed, then it will display the output.

import subprocess

out = subprocess.run(['ping', '-c', '3', '127.0.0.1'], capture_output=True, text=True)
print(out.stdout)

I don't have this problem without capture_output=False

out = subprocess.run(['ping', '-c', '3', '127.0.0.1'], text=True)

Would it be possible to make it in real time with capture_output=True?

Imagine if I change it to 'ping', '-c', '100', I've to wait it like forever in order to see the output.

If subprocess.run is not the right tool to achieve this, please let me know the alternative.

Sabrina
  • 2,042
  • 3
  • 20
  • 35
  • What is expected behaviour of making the script 'real time'? With subprocess you spawn a process, it produces stdout and you read this stdout in print statement. Do you want to be listening to stdout as it gets filled? – Evgeny Sep 23 '19 at 04:03
  • @triplee, that's totally different case. Mine is getting ping in real time so that I don't have to wait forever just to see the final output. – Sabrina Sep 23 '19 at 04:14
  • How exactly is yours different? Sounds exactly the same to me. `run` does indeed wait for the subprocess to finish before returning. To interact with a running subprocess, you need raw `Popen` and then having to do the necessary management of the subprocess until its death. – tripleee Sep 23 '19 at 04:15

0 Answers0