refactor: use psutil uptime and wmi lib for Windows temps; graceful SIGINT/SIGTERM/SIGBREAK shutdown

This commit is contained in:
snowy-yang committed 2026-09-12 20:18:26 +08:00
1 parent e39d1a0f52
commit 593b8011c6
4 files changed
+70 -20

No files matched your search

+6 -16
View File
@@ -110,22 +110,16 @@ def get_memory() -> dict:
def _wmi_temps() -> dict:
"""Windows: 通过 WMI 热区读取温度(部分主板支持,可能需要管理员权限)。"""
import subprocess
try:
out = subprocess.run(
["powershell", "-NoProfile", "-Command",
"Get-CimInstance -Namespace root/wmi -ClassName MSAcpi_ThermalZoneTemperature "
"| Select-Object -ExpandProperty CurrentTemperature"],
capture_output=True, text=True, timeout=10,
encoding="utf-8", errors="replace",
)
values = [float(x) for x in out.stdout.split() if x.replace(".", "", 1).isdigit()]
except (OSError, subprocess.SubprocessError, ValueError):
import wmi # 仅 Windows 安装
zones = wmi.WMI(namespace="root\\wmi").MSAcpi_ThermalZoneTemperature()
values = [z.CurrentTemperature for z in zones]
except Exception: # 类不存在 / 权限不足 / 非 Windows
return {}
# WMI 单位是 0.1 开尔文
return {"ThermalZone": [
{"label": f"zone{i}", "current": round(v / 10 - 273.15, 1)}
for i, v in enumerate(values)
for i, v in enumerate(values) if v
]}
@@ -249,11 +243,7 @@ def get_top_memory(n: int = 10) -> list:
def get_uptime() -> float:
try:
with open("/proc/uptime") as f:
return float(f.read().split()[0])
except OSError:
return time.time() - psutil.boot_time()
return time.time() - psutil.boot_time()
def get_status(top_n: int | None = None) -> dict: