系统资源实时监控脚本(python) 本文共有1674个字,关键词: 该脚本会自动检测系统环境,支持NVIDIA显卡的GPU信息获取,并会处理没有GPU的情况。显示信息使用GB作为显存单位,百分比数据保留1位小数 ``` import time import os try: import psutil except ImportError: print("请先安装psutil库:pip install psutil") exit() try: import GPUtil except ImportError: print("请先安装GPUtil库:pip install gputil") exit() def get_gpu_info(): try: gpus = GPUtil.getGPUs() if gpus: gpu = gpus[0] return { "load": gpu.load * 100, "memory_used": gpu.memoryUsed / 1024, "memory_total": gpu.memoryTotal / 1024 } return None except Exception as e: return None def get_memory_info(): mem = psutil.virtual_memory() return { "used": mem.used / (1024**3), # 转换为GB "total": mem.total / (1024**3), "percent": mem.percent } while True: # 获取系统信息 cpu_usage = psutil.cpu_percent(interval=2) mem_info = get_memory_info() gpu_info = get_gpu_info() # 清屏 os.system('cls' if os.name == 'nt' else 'clear') # 显示系统信息 print("=== 系统资源监控(每隔2秒刷新) ===") print(f"CPU使用率: {cpu_usage:.1f}%") print(f"内存使用: {mem_info['used']:.1f}/{mem_info['total']:.1f} GB ({mem_info['percent']:.1f}%)") if gpu_info: mem_percent = (gpu_info["memory_used"] / gpu_info["memory_total"]) * 100 print(f"GPU使用率: {gpu_info['load']:.1f}%") print(f"显存使用: {gpu_info['memory_used']:.1f}/{gpu_info['memory_total']:.1f} GB ({mem_percent:.1f}%)") else: print("GPU信息: 未检测到可用GPU") print("\n(按Ctrl+C退出监控)") ``` 「一键投喂 软糖/蛋糕/布丁/牛奶/冰阔乐!」 赞赏 × 梦白沙 (๑>ڡ<)☆谢谢老板~ 1元 2元 5元 10元 50元 任意金额 2元 使用微信扫描二维码完成支付 版权声明:本文为作者原创,如需转载须联系作者本人同意,未经作者本人同意不得擅自转载。 ubuntu系统,Python 2025-02-23 评论 1 次浏览