Last active
July 26, 2025 17:49
-
-
Save tmr232/4a10e17ddf4aefcc0c94a15bdddc58f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| from pathlib import Path | |
| import typer | |
| import maya | |
| import psutil | |
| from loguru import logger | |
| app = typer.Typer() | |
| def human_readable_size(size, decimal_places=2): | |
| for unit in ["B", "KiB", "MiB", "GiB", "TiB", "PiB"]: | |
| if size < 1024.0 or unit == "PiB": | |
| break | |
| size /= 1024.0 | |
| return f"{size:.{decimal_places}f} {unit}" | |
| @app.command() | |
| def memlog(outpath: Path = Path("./.memlog"), frequency: int = 1): | |
| with outpath.open("a+") as f: | |
| print_counter = int(frequency) | |
| current_print_counter = print_counter | |
| sleep_duration = 1 / frequency | |
| while 1: | |
| timestamp = maya.now().iso8601() | |
| memory_used = psutil.virtual_memory().used | |
| cpu_percent = psutil.cpu_percent() | |
| f.write(f"{timestamp}, {memory_used}, {cpu_percent}%\n") | |
| if current_print_counter >= print_counter: | |
| logger.info(f"{human_readable_size(memory_used)}, {cpu_percent}%") | |
| current_print_counter = 0 | |
| current_print_counter += 1 | |
| time.sleep(sleep_duration) | |
| if __name__ == "__main__": | |
| app() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typer | |
| maya | |
| psutil | |
| loguru |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment