Recursing for Progress Bar

H

half.italian

I'm making a small interface for copying large groups of files around a
filesystem. I have a progressbar that counts the items in the
destination, and increments as each new file is copied over. It
compares this number to the number of files in the source and updates
accordingly.

All is fine and dandy with an average amount of files (<20000), but
when the amount of files to be copied becomes large, I end up getting
"Maximum recurssion depth exceeded" errors. I found out I could find
an safe recursion limit per system, and then set the recursion limit
using sys.setrecursionlimit(), but its still not giving me the depth I
would like. I can also make it work by slowing the update speed way
down, but it just looks clumsy and even then, the top limit could be 10
times the amount of data I'm testing with now...maybe even 200GB. I
don't really know how slow I would need it to update to make sure to
stay below the limit.

Is there a way to get around recursion limits? Help!

~half.italian

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

def incrementProgress(self, window, workorder):
dest_count = workorder.copyIndex
self.label.config(text=workorder.movingFile)
src_count = len(workorder.contents)

if self.p >= 100:
window.destroy()
#
# RESET self.p so future moves work!!
self.p = 0
return

# check for an empty workorder folder...it's already been moved
#
if workorder.contents == []:
window.destroy()
self.p = 0
return


self.p = (float(dest_count)/float(src_count))*100
print "Percentage copied:", self.p

self.progressBar.updateProgress(self.p)

time.sleep(.1)
self.incrementProgress(window, workorder)
 
B

Ben Finney

Is there a way to get around recursion limits? Help!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

def incrementProgress(self, window, workorder):
#...
time.sleep(.1)
self.incrementProgress(window, workorder)

You don't show a complete working example, so it's hard to know the
context of this.

What is the purpose of this function? Why is it performing these last
two lines at all?

Surely this function should be called by an *external* loop of the
actual processing, with no recursion, and no enforced delay.

import time

class ProgressBar(object):
""" Display of progress as a horizontal bar """

update_frequency = 0.1

def __init__(self, window):
self.window = window
self._prev_increment_time = 0

def increment(self, workorder):
""" Increment the progress display """
# ... code to unconditionally update the display
self._prev_update_time = time.time()

def update(self, workorder):
""" Update the progress if necessary """
time_since_update = time.time() - self._prev_update_time
if time_since_update >= self.update_frequency:
self.increment(workorder)

window = however_you_make_a_window()
bar = ProgressBar(window)

for foo in iterator_of_foos:
workorder = do_the_processing(foo)
bar.update(workorder)
 
D

Dennis Lee Bieber

Is there a way to get around recursion limits? Help!
Yes... restructure your code to not use recursion...

Should be easy since your incomplete snippet implied that there is
NO RETURN VALUE. Recursion is often used when the value of a computation
has to be fed to the next higher level of the calculation (the infamous
factorial:

fact(6) = 6 * fact(6-1)
5 * fact(5-1)
4 * fact(4-1)
3 * fact(3-1)
2 * fact(2-1)
1 (by definition: fact(1) = 1)
2
6
24
120
720
def incrementProgress(self, window, workorder):
while self.p < 100 and workorder.contents:
dest_count = workorder.copyIndex
self.label.config(text=workorder.movingFile)
src_count = len(workorder.contents)
self.p = (float(dest_count) / float(src_count)) * 100.0
print "Percentage copied:", self.p
self.progressBar.updateProgress(self.p)
time.sleep(0.1)

window.destroy()
self.p = 0
return


--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
H

half.italian

DOLT!

Thanks!
Yes... restructure your code to not use recursion...

Should be easy since your incomplete snippet implied that there is
NO RETURN VALUE. Recursion is often used when the value of a computation
has to be fed to the next higher level of the calculation (the infamous
factorial:

fact(6) = 6 * fact(6-1)
5 * fact(5-1)
4 * fact(4-1)
3 * fact(3-1)
2 * fact(2-1)
1 (by definition: fact(1) = 1)
2
6
24
120
720

while self.p < 100 and workorder.contents:
dest_count = workorder.copyIndex
self.label.config(text=workorder.movingFile)
src_count = len(workorder.contents)
self.p = (float(dest_count) / float(src_count)) * 100.0
print "Percentage copied:", self.p
self.progressBar.updateProgress(self.p)
time.sleep(0.1)

window.destroy()
self.p = 0
return


--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
H

half.italian

Makes perfect sense. Sometimes it takes being whacked to see it the
right way.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top