2

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.

3
  • 1
    You've got to decide on your own scale and work out what the figures mean yourself. Without a lot more information, we can't help. Commented Sep 19, 2011 at 7:23
  • i want to say that how can i set the maxval and that range prior execution it should take some delay time as a count something like that i am not sure.. Commented Sep 19, 2011 at 7:32
  • Set the maxval to the size of the file and increment the position when reading it. Commented Sep 19, 2011 at 7:47

2 Answers 2

3

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.