Skip to content

Avoid rendering on highly frequent updates in progressbar #676

@tilsche

Description

@tilsche

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions