-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Closed
Labels
Milestone
Description
I am using a progressbar in a loop with short iterations, which negatively affects performance. It would be good to supply a chunk size such that the heavy part of an update is only performed every nth time.
Currently I am using a small wrapper around it:
class ChunkProgressbar:
def __init__(self, iterator, chunk_size=100, enabled=True, **kwargs):
self.iterator = iterator
self.chunk_size = chunk_size
self.bar = progressbar(iterator, **kwargs)
self.missed_updates = 0
def __enter__(self):
if self.bar is not None:
self.bar.__enter__()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.bar.update(self.missed_updates)
self.bar.__exit__(exc_type, exc_val, exc_tb)
def __iter__(self):
for index, elem in enumerate(self.iterator):
yield elem
self.missed_updates += 1
if index % self.chunk_size == self.chunk_size - 1:
self.bar.update(self.missed_updates)
self.missed_updates = 0
One could also throttle the rendering based on timing information (like the eta calculation), but that might be costly again due to timing calls or mess with heterogeneous iteration times.
Reactions are currently unavailable