TAGS :Viewed: 11 - Published at: a few seconds ago

[ Use of python progress bar in script ]

I want to show progress bar in my script because it takes a lots of time to execute while working on huge files.I gone through the python progressbar module

and examples also its good and very intresting to use but as per examples all values are predefine .As we can't guess the max execution time of programm or function.So i am not able to figure out how should i use progress bar function in my sctipt

for data in files:
     crawl_data(data)

this is the crawl_data function which take time so how can i set the progress bar values

pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval=300).start()
for i in range(300):
    time.sleep(0.01)
    pbar.update(i+1)
pbar.finish()

how can i define this range and maxval values in above lines of code.

Answer 1


This is what I got working.

Stdout

Working: | Elapsed Time: 0:00:10  

Python

import time
import progressbar
import threading

def crawl_data(data):
    # sleep for 10 seconds
    time.sleep(10)
# end of def crawl_data

def main():
    data = 'some data'
    widgets = ['Working: ', progressbar.AnimatedMarker(), ' ',
                   progressbar.Timer()]
    pbar = progressbar.ProgressBar(widgets=widgets)
    # creating thread to run crawl_data()
    thread = threading.Thread(target=crawl_data,
                              args=(data,))
    thread.daemon = True
    # starting thread and progress bar
    thread.start()
    pbar.start()
    i = 1
    # continuous loop until crawl_data thread is not alive
    while True:
        # update every second
        time.sleep(1)
        pbar.update(i)
        if not thread.is_alive():
            pbar.finish()
            break
        # end of if thread is not alive
        i += 1
    # end of continuous loop until crawl_data thread is not alive
    # prints a new line
    print
# end of def main

# run main
main()

Answer 2


If you can't guess the execution time, a progress bar is worthless (remember most of the old MS progress bars?). You are probably looking for something like a activity indicator. Since web2.0 it is common to use something rotating.