Printing progress bar on a console without the use of for -loop

2024/11/15 0:32:39

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?

Answer

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.

https://en.xdnf.cn/q/119352.html

Related Q&A

ModuleNotFoundError: No module named verovio

Hi there I would like to run my flask app in a container but I got stucked caused of a third party module. (I am using PyCharm)This is my docker file:FROM python:3-alpineMAINTAINER fooCOPY app /appWORK…

Python: TypeError: list object is not callable on global variable

I am currently in the process of programming a text-based adventure in Python as a learning exercise. I want "help" to be a global command, stored as values in a list, that can be called at (…

Python beautifulsoup how to get the line after href

I have this piece of html:<a href="http://francetv.fr/videos/alcaline_l_instant_,12163184.html" class="ss-titre">"Paris Combo" </a> <…

Scrapy empty output

I am trying to use Scrapy to extract data from page. But I get an empty output. What is the problem? spider: class Ratemds(scrapy.Spider):name = ratemdsallowed_domains = [ratemds.com]custom_settings =…

nested classes - how to use function from parent class?

If I have this situation:class Foo(object):def __init__(self):self.bar = Bar()def do_something(self):print doing somethingclass Bar(object):def __init(self):self.a = adef some_function(self):I want to …

CUDA Function Wont Execute For Loop on Python with Numba

Im trying to run a simple update loop of a simulation on the GPU. Basically there are a bunch of "creatures" represented by circles that in each update loop will move and then there will be a…

Implementing the Ceaser Cipher function through input in Python

Im trying to create a Ceaser Cipher function in Python that shifts letters based off the input you put in.plainText = input("Secret message: ") shift = int(input("Shift: "))def caes…

Twitter scraping of older tweets

I am doing a project in which I needed to get tweets from twitter, and I used the twitter API but it only gives tweets from 7-9 days old but I want a few months older tweets as well. So I decided to sc…

Bootstrap Navbar Logo not found

Hello I am trying to get my NavBar on bootstrap to show a Logo, I have tried moving the png to different folders in the app but I get this error: System check identified no issues (0 silenced). January…

Why camelcase not installed?

i try to install camelcase in my python project. pip install camelcase but when i want to use the package, pylance give me this error: Import "camelcase" could not be resolved Pylance (report…