Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display meter instead of progressbar #374

Closed
nvdv opened this issue Apr 14, 2017 · 4 comments · Fixed by #738
Closed

Display meter instead of progressbar #374

nvdv opened this issue Apr 14, 2017 · 4 comments · Fixed by #738
Assignees
Labels
p3-enhancement 🔥 Much new such feature

Comments

@nvdv
Copy link

nvdv commented Apr 14, 2017

First of all, thanks for the great tool!
What is the recommended way to use tqdm progressbar as meter (e.g. process memory usage)?
I've tried

meter.n = <custom_value>
meter.refresh()

and It seems to work, but what is the recommended way?
Thanks!

@casperdcl
Copy link
Sponsor Member

Hi @nvdv. I've never thought of a resource monitor use case. I guess you should set the bar format to exclude ETA and rate because that wouldn't make sense for a monitor. But the code you wrote sounds right.

@nvdv
Copy link
Author

nvdv commented Apr 19, 2017

yep, it works.
Also you might consider this question as feature request 😄

@LankyCyril
Copy link

Not sure how generalizeable this is, but I roll my own tiny class that inherits from tqdm when I want a progress bar that reports RAM usage with every update:

from tqdm import tqdm
from os import getpid
from psutil import Process

class ramqdm(tqdm):
    """tqdm progress bar that reports RAM usage with each update"""
    _empty_desc = "using ? GB (? GiB) RAM"
    _desc = "using {:.2f} GB ({:.2f} GiB) RAM"
    _GB = 10**9
    _GiB = 2**30
    """"""
    def __init__(self, *args, **kwargs):
        """Override desc and get reference to current process"""
        if "desc" in kwargs:
            # prepend desc to the reporter mask:
            self._empty_desc = kwargs["desc"] + " " + self._empty_desc
            self._desc = kwargs["desc"] + " " + self._desc
            del kwargs["desc"]
        else:
            # nothing to prepend, reporter mask is at start of sentence:
            self._empty_desc = self._empty_desc.capitalize()
            self._desc = self._desc.capitalize()
        super().__init__(*args, desc=self._empty_desc, **kwargs)
        self._process = Process(getpid())
    """"""
    def update(self):
        """Calculate RAM usage and update progress bar"""
        rss = self._process.memory_info().rss
        current_desc = self._desc.format(rss/self._GB, rss/self._GiB)
        self.set_description(current_desc)
        super().update()

and then update it manually from within my loops (my use cases have always been such that I couldn't wrap an iterable with tqdm() or ramqdm() anyway).

If anyone's interested and has suggestions how to expand on this, I could develop it further and make a pull request...

casperdcl added a commit that referenced this issue May 10, 2019
- example of dynamic usage (#735, #545, #547, #432, #374)
- note writing issues #737
- update badges
@casperdcl
Copy link
Sponsor Member

@LankyCyril happy to accept a PR

@casperdcl casperdcl mentioned this issue May 10, 2019
13 tasks
@ghost ghost assigned casperdcl May 10, 2019
@ghost ghost added the review label May 10, 2019
@ghost ghost removed the review label May 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-enhancement 🔥 Much new such feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants