在CentOS系统中,可通过以下工具和方法监控Python资源:
import psutil
# 示例:获取CPU和内存使用率
cpu_percent = psutil.cpu_percent(interval=1)
memory_info = psutil.virtual_memory()
print(f"CPU使用率: {cpu_percent}%")
print(f"内存使用率: {memory_info.percent}%")
-w
参数启动Web界面。# 安装与启动
sudo yum install epel-release
sudo yum install glances
glances # 命令行监控
glances -w # Web界面监控
prometheus_client
库暴露Python应用指标,Grafana可视化数据。pip install py-spy
py-spy top --pid <进程ID> # 实时查看进程性能
pip install dstat
dstat -ta 1 # 每秒刷新一次系统资源数据
根据需求选择工具,可实现从基础资源监控到深度性能分析的全覆盖。