A convenience module for shelling out with realtime output
Credit: Largely taken from https://www.endpoint.com/blog/2015/01/28/getting-realtime-output-using-python
When I launch a long running unix process within a python script, it waits until the process is finished, and only then do I get the complete output of my program. This is annoying if I’m running a process that takes a while to finish. And I want to capture the output and display it in the nice manner with clear formatting.
To run a process and read all of its output, set the stdout value to PIPE and call communicate()
.
import subprocess
process = subprocess.Popen(['echo', '"Hello stdout"'], stdout=subprocess.PIPE)
stdout = process.communicate()[0]
print('STDOUT:{}'.format(stdout))
The above script will wait for the process to complete and then it will display the output.