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

fix(collector): fix documentation for ResourceMetricCollector.clear() function #132

Merged
merged 5 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fix documentation for the `ResourceMetricCollector.clear()` method by [@MyGodItsFull0fStars](https://github.com/MyGodItsFull0fStars) in [#132](https://github.com/XuehaiPan/nvitop/pull/132).
- Gracefully ignore UTF-8 decoding errors by [@XuehaiPan](https://github.com/XuehaiPan).

### Removed
Expand Down
16 changes: 9 additions & 7 deletions nvitop/api/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class ResourceMetricCollector: # pylint: disable=too-many-instance-attributes

collector.activate(tag='<tag>') # alias: start
collector.deactivate() # alias: stop
collector.reset(tag='<tag>')
collector.clear(tag='<tag>')
collector.collect()

with collector(tag='<tag>'):
Expand Down Expand Up @@ -539,14 +539,14 @@ def context(self, tag: str) -> Generator[ResourceMetricCollector]:
__call__ = context # alias for `with collector(tag='<tag>')`

def clear(self, tag: str | None = None) -> None:
"""Reset the metric collection with the given tag.
"""Clear the metric collection with the given tag.

If the tag is not specified, reset the current active collection. For nested collections,
the sub-collections will be reset as well.
If the tag is not specified, clear the current active collection. For nested collections,
the sub-collections will be cleared as well.

Args:
tag (Optional[str]):
The tag to reset. If :data:`None`, the current active collection will be reset.
The tag to clear. If :data:`None`, the current active collection will be reset.

Examples:
>>> collector = ResourceMetricCollector()
Expand All @@ -558,12 +558,12 @@ def clear(self, tag: str | None = None) -> None:
... time.sleep(5.0)
... collector.collect() # metrics within the cumulative 10.0s interval
...
... collector.reset() # reset the active collection
... collector.clear() # clear the active collection
... time.sleep(5.0)
... collector.collect() # metrics within the 5.0s interval
...
... with collector(tag='batch'): # key prefix -> 'train/batch'
... collector.reset(tag='train') # reset both 'train' and 'train/batch'
... collector.clear(tag='train') # clear both 'train' and 'train/batch'
"""
with self._lock:
if self._metric_buffer is None:
Expand All @@ -585,6 +585,8 @@ def clear(self, tag: str | None = None) -> None:
break
buffer = buffer.prev # type: ignore[assignment]

reset = clear

def collect(self) -> dict[str, float]:
"""Get the average resource consumption during collection."""
with self._lock:
Expand Down