如何在Python中获取当前系统状态(当前CPU、RAM、空闲磁盘空间等)?理想情况下,它可以同时适用于Unix和Windows平台。
从我的搜索中似乎有一些可能的方法:
使用像PSI这样的库(目前似乎没有积极开发,在多个平台上也不支持)或像pystatgrab这样的库(从2007年开始似乎没有活动,也不支持Windows)。
使用平台特定的代码,例如使用os.popen("ps")或*nix系统的类似代码,以及ctypes.windll中的MEMORYSTATUS。Windows平台的kernel32(请参阅ActiveState上的配方)。可以将所有这些代码片段放在一个Python类中。
这并不是说这些方法不好,而是是否已经有一种支持良好的多平台方式来做同样的事情?
从第一反应中获得反馈,并做一些小的改变
#!/usr/bin/env python
#Execute commond on windows machine to install psutil>>>>python -m pip install psutil
import psutil
print (' ')
print ('----------------------CPU Information summary----------------------')
print (' ')
# gives a single float value
vcc=psutil.cpu_count()
print ('Total number of CPUs :',vcc)
vcpu=psutil.cpu_percent()
print ('Total CPUs utilized percentage :',vcpu,'%')
print (' ')
print ('----------------------RAM Information summary----------------------')
print (' ')
# you can convert that object to a dictionary
#print(dict(psutil.virtual_memory()._asdict()))
# gives an object with many fields
vvm=psutil.virtual_memory()
x=dict(psutil.virtual_memory()._asdict())
def forloop():
for i in x:
print (i,"--",x[i]/1024/1024/1024)#Output will be printed in GBs
forloop()
print (' ')
print ('----------------------RAM Utilization summary----------------------')
print (' ')
# you can have the percentage of used RAM
print('Percentage of used RAM :',psutil.virtual_memory().percent,'%')
#79.2
# you can calculate percentage of available memory
print('Percentage of available RAM :',psutil.virtual_memory().available * 100 / psutil.virtual_memory().total,'%')
#20.8
您可以使用命令pip install SystemScripter来使用最近发布的SystemScripter库。这个库使用其他库(如psutil)创建一个完整的系统信息库,涵盖从CPU到磁盘的信息。
对于当前的CPU使用率使用函数:
SystemScripter.CPU.CpuPerCurrentUtil(SystemScripter.CPU()) #class init as self param if not work
这是使用百分比或使用情况:
SystemScripter.CPU.CpuCurrentUtil(SystemScripter.CPU())
https://pypi.org/project/SystemScripter/#description
关于CPU的详细信息,请使用psutil库
https://psutil.readthedocs.io/en/latest/#cpu
对于RAM频率(以MHz为单位),使用内置的Linux库dmidecode并操作输出位;)。此命令需要root权限,因此也需要提供您的密码。只需复制以下推荐替换mypass与您的密码
进口操作系统
操作系统。system("echo mpass | sudo -S dmidecode -t memory | grep 'Clock Speed' | cut -d ':' -f2")
------------------- 输出 ---------------------------
1600吨/秒
未知的
1600吨/秒
未知的0
更具体的
[i在os中的i。]popen("echo mpass | sudo -S dmidecode -t memory | grep 'Clock Speed' | cut -d ':' -f2").read()。if i.isdigit()]
-------------------------- 输出 -------------------------
[' 1600 ', ' 1600 ']
这是所有好东西的汇总:
psutil + os获得Unix和Windows兼容性:
这允许我们得到:
CPU
内存
磁盘
代码:
import os
import psutil # need: pip install psutil
In [32]: psutil.virtual_memory()
Out[32]: svmem(total=6247907328, available=2502328320, percent=59.9, used=3327135744, free=167067648, active=3671199744, inactive=1662668800, buffers=844783616, cached=1908920320, shared=123912192, slab=613048320)
In [33]: psutil.virtual_memory().percent
Out[33]: 60.0
In [34]: psutil.cpu_percent()
Out[34]: 5.5
In [35]: os.sep
Out[35]: '/'
In [36]: psutil.disk_usage(os.sep)
Out[36]: sdiskusage(total=50190790656, used=41343860736, free=6467502080, percent=86.5)
In [37]: psutil.disk_usage(os.sep).percent
Out[37]: 86.5
你可以在subprocess中使用psutil或psmem
示例代码
import subprocess
cmd = subprocess.Popen(['sudo','./ps_mem'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
out,error = cmd.communicate()
memory = out.splitlines()
参考
https://github.com/Leo-g/python-flask-cmd