I would like to display and parse the output of a longer process. As an example, I would like to to run apt-get update
and display a progress bar while it's running.
So far I am using this code, which works reliably and returns stdout, stderr and returncode once the process finishes.
def run(cmd):
child = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = child.communicate()
returncode = child.returncode
return stdout, stderr, returncode
My problem is that I would like to display some kind of progress-bar (such as a simple line counter) or display stdout and stderr to the user while the process is still running.
I've read the following questions, however it's still not clear to me how can I do it:
- showing progress while spawning and running subprocess
- Getting progress message from a subprocess
- live output from subprocess command
Do I need some kind of timer with a sleep function inside a while loop? Or I should use multithreading?