-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Comments
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. |
yep, it works. |
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... |
@LankyCyril happy to accept a PR |
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
and It seems to work, but what is the recommended way?
Thanks!
The text was updated successfully, but these errors were encountered: