如何在Python中获取当前系统状态(当前CPU、RAM、空闲磁盘空间等)?理想情况下,它可以同时适用于Unix和Windows平台。

从我的搜索中似乎有一些可能的方法:

使用像PSI这样的库(目前似乎没有积极开发,在多个平台上也不支持)或像pystatgrab这样的库(从2007年开始似乎没有活动,也不支持Windows)。 使用平台特定的代码,例如使用os.popen("ps")或*nix系统的类似代码,以及ctypes.windll中的MEMORYSTATUS。Windows平台的kernel32(请参阅ActiveState上的配方)。可以将所有这些代码片段放在一个Python类中。

这并不是说这些方法不好,而是是否已经有一种支持良好的多平台方式来做同样的事情?


当前回答

您可以使用命令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

其他回答

psutil库提供了各种平台上关于CPU、RAM等的信息:

psutil是一个模块,提供了一个接口,通过使用Python以可移植的方式检索正在运行的进程和系统利用率(CPU,内存)的信息,实现了ps、top和Windows任务管理器等工具提供的许多功能。 它目前支持Linux, Windows, OSX, Sun Solaris, FreeBSD, OpenBSD和NetBSD, 32位和64位架构,Python版本从2.6到3.5 (Python 2.4和2.5的用户可能使用2.1.3版本)。


一些例子:

#!/usr/bin/env python
import psutil
# gives a single float value
psutil.cpu_percent()
# gives an object with many fields
psutil.virtual_memory()
# you can convert that object to a dictionary 
dict(psutil.virtual_memory()._asdict())
# you can have the percentage of used RAM
psutil.virtual_memory().percent
79.2
# you can calculate percentage of available memory
psutil.virtual_memory().available * 100 / psutil.virtual_memory().total
20.8

以下是其他文档,提供了更多的概念和感兴趣的概念:

https://psutil.readthedocs.io/en/latest/

“…当前系统状态(当前CPU、RAM、空闲磁盘空间等)”“*nix和Windows平台”可能是一个很难实现的组合。

操作系统在管理这些资源的方式上有根本的不同。实际上,它们在核心概念上有所不同,比如定义什么是系统,什么是应用程序时间。

“空闲磁盘空间”?什么算“磁盘空间”?所有设备的所有分区?多引导环境中的外部分区呢?

我不认为Windows和*nix之间有足够明确的共识来实现这一点。事实上,在被称为Windows的各种操作系统之间甚至可能没有任何共识。有没有一个Windows API同时适用于XP和Vista?

从第一反应中获得反馈,并做一些小的改变

#!/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

你可以读取/proc/meminfo来获得使用的内存

file1 = open('/proc/meminfo', 'r') 

for line in file1: 
    if 'MemTotal' in line: 
        x = line.split()
        memTotal = int(x[1])
        
    if 'Buffers' in line: 
        x = line.split()
        buffers = int(x[1])
        
    if 'Cached' in line and 'SwapCached' not in line: 
        x = line.split()
        cached = int(x[1])
    
    if 'MemFree' in line: 
        x = line.split()
        memFree = int(x[1])

file1.close()

percentage_used = int ( ( memTotal - (buffers + cached + memFree) ) / memTotal * 100 )
print(percentage_used)