I have a script written in python, where I have a statement:
Process.open() //some parameters
Which executes a command and puts the output on the console ,
where I do not know the time taken to execute the above statement , the above statement will be called by a function to complete a series of command execution and there is no for loop used at all like in typical examples in “progress bar in python examples”.
Now , my question is it possible to print the progress bar to show the completion of progress like 1..100% in python for this scenario?
The subprocess
module allows you to attach pipes to the stdout and stderr of your spawned process. It's a bit complicated but if you know the output of that process very well, you can poll the output generated through the pipe at specific intervals and then increment a progressbar accordingly. Then again, if that process generates console output anyway, why not implement a progressbar there?
Edit to show how this could be done. args
is a list with your command and its arguments.
import subprocess
sub = subprocess.Popen(args, bufsize=-1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
while sub.poll() is None:output = sub.stdout.read() # get all of current contentsif output:# ... process output and decide how far the process has advanced# ... advance your progressbar accordinglyelse:time.sleep(1E-01) # decide on a reasonable number of seconds to wait before polling again
if sub.returncode != 0:err = sub.stderr.read()# ... decide what to do
If you can modify the sub process I suggest you do the progress there.