如何在Python中获取当前CPU和RAM使用情况?

496

如何在Python中获取当前系统状态(当前CPU、RAM、可用磁盘空间等)?最好能在Unix和Windows平台上都能使用。

我在搜索中发现有几种可能的提取方式:

  1. 使用类似于PSI(看起来目前没有积极开发并且在多个平台上不受支持)或者类似于pystatgrab(似乎自2007年以来没有活动,也不支持Windows)的库。

  2. 使用特定于平台的代码,例如对于*nix系统使用os.popen("ps")或类似方法,对于Windows平台使用ctypes.windll.kernel32中的MEMORYSTATUS(参见ActiveState上的此代码示例)。可以将所有这些代码片段放在一个Python类中。

这些方法并不差,但是否已经有一个良好支持、跨平台的方式来完成同样的事情呢?


你可以使用动态导入来构建自己的多平台库:“如果 sys.platform == 'win32':import win_sysstatus as sysstatus; else”... - John Fouhy
1
在App Engine上也有一个能够工作的东西会很酷。 - Attila O.
1
软件包的年龄是否重要?如果有人第一次就正确地获取了它们,为什么它们不仍然是正确的呢? - Paul Smith
21个回答

593

psutil库 可以在多种平台上提供CPU、内存等信息:

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

以下是其他提供更多概念和有趣概念的文档:


42
在我的苹果电脑上成功运行:$ pip install psutil; >>> import psutil; psutil.cpu_percent()>>> psutil.virtual_memory(),它们会返回一个漂亮的虚拟内存对象:vmem(total=8589934592L, available=4073336832L, percent=52.6, used=5022085120L, free=3560255488L, active=2817949696L, inactive=513081344L, wired=1691054080L) - hobs
16
没有psutil库,怎么做这件事? - BigBrownBear00
5
Python内置了一个名为resource的库。然而,它似乎最多只能获取单个Python进程及其子进程所使用的内存,并且似乎不是很准确。一次快速测试显示,相对于Mac实用工具,resource的误差约为2MB。 - Austin A
15
@BigBrownBear00,请查看psutil的源代码 ;) - Mehulkumar
1
@Jon Cage 你好Jon,我想请教一下free memory和available memory之间的区别。我打算使用psutil.virtual_memory()来确定我可以加载多少数据进行分析。感谢你的帮助! - AiRiFiEd
显示剩余5条评论

98

使用psutil库。在Ubuntu 18.04上,截至2019年1月30日,通过pip安装了最新版本5.5.0。较旧版本的行为可能会有所不同。 您可以通过以下Python代码检查psutil版本:

from __future__ import print_function  # for Python2
import psutil
print(psutil.__versi‌​on__)

获取一些内存和CPU统计信息:

from __future__ import print_function
import psutil
print(psutil.cpu_percent())
print(psutil.virtual_memory())  # physical memory usage
print('memory % used:', psutil.virtual_memory()[2])

virtual_memory(元组)将显示系统范围内使用的内存百分比。 在我的Ubuntu 18.04上,这似乎被高估了几个百分点。

您还可以获取当前Python实例使用的内存:

import os
import psutil
pid = os.getpid()
python_process = psutil.Process(pid)
memoryUse = python_process.memory_info()[0]/2.**30  # memory use in GB...I think
print('memory use:', memoryUse)

它将显示您的Python脚本当前的内存使用情况。

psutil的pypi页面上还有一些更深入的示例。


3
请勿将变量命名为“py”。 - MrR
我知道现在这不是最佳实践,但py并不是关键字或其他什么。除了不是一个描述性的变量名之外,你说不使用py还有其他原因吗? - wordsforthewise
6
在许多其他场合中,它被普遍用来表示“与Python相关的东西”,例如redis-py。我不会使用两个字母的“py”来表示当前的进程。 - MrR

65

通过结合 tqdmpsutil,可以实时监测 CPU 和 RAM 的使用情况。在运行大量计算/处理时非常有用。

CLI CPU 和 RAM 使用情况进度条

它还可以在 Jupyter 中使用,而无需进行任何代码更改:

Jupyter CPU 和 RAM 使用情况进度条

from tqdm import tqdm
from time import sleep
import psutil

with tqdm(total=100, desc='cpu%', position=1) as cpubar, tqdm(total=100, desc='ram%', position=0) as rambar:
    while True:
        rambar.n=psutil.virtual_memory().percent
        cpubar.n=psutil.cpu_percent()
        rambar.refresh()
        cpubar.refresh()
        sleep(0.5)

使用multiprocessing 库将进度条放入单独的进程中是非常方便的。

此代码片段也可在gist中找到。


55

仅适用于Linux: 使用标准库依赖性的RAM使用情况一行代码:

import os
tot_m, used_m, free_m = map(int, os.popen('free -t -m').readlines()[-1].split()[1:])

3
非常有用!为了直接获得以人类可读的单位表示的结果:os.popen('free -th').readlines()[-1].split()[1:]。请注意,该行代码返回一个字符串列表。 - iipr
2
python:3.8-slim-buster 没有 free - Martin Thoma
请看这里,@MartinThoma:https://dev59.com/questions/V1oV5IYBdhLWcg3wErWD。 - Mr. Duhart
used_m和free_m的总和不等于tot_m。而且结果也与htop不匹配。我理解错了什么? - figs_and_nuts
@MiloMinderbinder 这个的结果是交换空间和内存。如果你只想要内存,使用.readlines()[-2]而不是-1。 - some random nerd

31
以下代码没有使用外部库,适用于IT技术相关内容。我在Python 2.7.9上进行了测试。 CPU使用率
import os
    
CPU_Pct=str(round(float(os.popen('''grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage }' ''').readline()),2))
print("CPU Usage = " + CPU_Pct)  # print results

内存使用情况,总量、已用和可用

import os
mem=str(os.popen('free -t -m').readlines())
"""
Get a whole line of memory output, it will be something like below
['             total       used       free     shared    buffers     cached\n', 
'Mem:           925        591        334         14         30        355\n', 
'-/+ buffers/cache:        205        719\n', 
'Swap:           99          0         99\n', 
'Total:        1025        591        434\n']
 So, we need total memory, usage and free memory.
 We should find the index of capital T which is unique at this string
"""
T_ind=mem.index('T')
"""
Than, we can recreate the string with this information. After T we have,
"Total:        " which has 14 characters, so we can start from index of T +14
and last 4 characters are also not necessary.
We can create a new sub-string using this information
"""
mem_G=mem[T_ind+14:-4]
"""
The result will be like
1025        603        422
we need to find first index of the first space, and we can start our substring
from from 0 to this index number, this will give us the string of total memory
"""
S1_ind=mem_G.index(' ')
mem_T=mem_G[0:S1_ind]
"""
Similarly we will create a new sub-string, which will start at the second value. 
The resulting string will be like
603        422
Again, we should find the index of first space and than the 
take the Used Memory and Free memory.
"""
mem_G1=mem_G[S1_ind+8:]
S2_ind=mem_G1.index(' ')
mem_U=mem_G1[0:S2_ind]

mem_F=mem_G1[S2_ind+8:]
print 'Summary = ' + mem_G
print 'Total Memory = ' + mem_T +' MB'
print 'Used Memory = ' + mem_U +' MB'
print 'Free Memory = ' + mem_F +' MB'

8
你不觉得使用Python中的字符串处理会更好地处理grepawk吗? - Reinderien
1
个人对awk不太熟悉,因此制作了一个无需awk的版本来处理下面的CPU使用率片段。非常方便,谢谢! - Jay
9
说这段代码没有使用外部库是不诚实的。实际上,它们对grep、awk和free的可用性有硬性依赖。这使得以上代码不具备可移植性。原帖中提到:“在*nix和Windows平台上加分。” - Captain Lepton

19

为了对您的程序进行逐行的内存和时间分析,我建议使用memory_profilerline_profiler

安装:

# Time profiler
$ pip install line_profiler
# Memory profiler
$ pip install memory_profiler
# Install the dependency for a faster analysis
$ pip install psutil

共同的部分是,通过使用相应的装饰器来指定要分析的函数。

例如:我在我的Python文件main.py中有几个函数需要分析。其中之一是linearRegressionfit()。我需要使用装饰器@profile,它可以帮助我根据时间和内存对代码进行分析。

对函数定义进行如下更改:

@profile
def linearRegressionfit(Xt,Yt,Xts,Yts):
    lr=LinearRegression()
    model=lr.fit(Xt,Yt)
    predict=lr.predict(Xts)
    # More Code

对于时间分析

运行:

$ kernprof -l -v main.py

输出

Total time: 0.181071 s
File: main.py
Function: linearRegressionfit at line 35

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    35                                           @profile
    36                                           def linearRegressionfit(Xt,Yt,Xts,Yts):
    37         1         52.0     52.0      0.1      lr=LinearRegression()
    38         1      28942.0  28942.0     75.2      model=lr.fit(Xt,Yt)
    39         1       1347.0   1347.0      3.5      predict=lr.predict(Xts)
    40                                           
    41         1       4924.0   4924.0     12.8      print("train Accuracy",lr.score(Xt,Yt))
    42         1       3242.0   3242.0      8.4      print("test Accuracy",lr.score(Xts,Yts))

进行 内存分析

执行:

$ python -m memory_profiler main.py

输出

Filename: main.py

Line #    Mem usage    Increment   Line Contents
================================================
    35  125.992 MiB  125.992 MiB   @profile
    36                             def linearRegressionfit(Xt,Yt,Xts,Yts):
    37  125.992 MiB    0.000 MiB       lr=LinearRegression()
    38  130.547 MiB    4.555 MiB       model=lr.fit(Xt,Yt)
    39  130.547 MiB    0.000 MiB       predict=lr.predict(Xts)
    40                             
    41  130.547 MiB    0.000 MiB       print("train Accuracy",lr.score(Xt,Yt))
    42  130.547 MiB    0.000 MiB       print("test Accuracy",lr.score(Xts,Yts))

另外,内存分析器结果也可以使用matplotlib进行绘制。

$ mprof run main.py
$ mprof plot

enter image description here 注意:测试版本如下

line_profiler 版本 == 3.0.2

memory_profiler 版本 == 0.57.0

psutil 版本 == 5.7.0


编辑:可以使用TAMPPA包来解析分析器的结果。通过它,我们可以得到逐行所需的绘图,如下图 plot


15

我们选择使用常规信息源,因为我们可以找到自由内存的瞬时波动,并且查询meminfo数据源是有帮助的。这也帮助我们获取了一些预解析的相关参数。

代码

import os

linux_filepath = "/proc/meminfo"
meminfo = dict(
    (i.split()[0].rstrip(":"), int(i.split()[1]))
    for i in open(linux_filepath).readlines()
)
meminfo["memory_total_gb"] = meminfo["MemTotal"] / (2 ** 20)
meminfo["memory_free_gb"] = meminfo["MemFree"] / (2 ** 20)
meminfo["memory_available_gb"] = meminfo["MemAvailable"] / (2 ** 20)

参考输出(我们去掉了所有换行符以进行进一步分析)

MemTotal: 1014500 kB MemFree: 562680 kB MemAvailable: 646364 kB Buffers: 15144 kB Cached: 210720 kB SwapCached: 0 kB Active: 261476 kB Inactive: 128888 kB Active(anon): 167092 kB Inactive(anon): 20888 kB Active(file): 94384 kB Inactive(file): 108000 kB Unevictable: 3652 kB Mlocked: 3652 kB SwapTotal: 0 kB SwapFree: 0 kB Dirty: 0 kB Writeback: 0 kB AnonPages: 168160 kB Mapped: 81352 kB Shmem: 21060 kB Slab: 34492 kB SReclaimable: 18044 kB SUnreclaim: 16448 kB KernelStack: 2672 kB PageTables: 8180 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 507248 kB Committed_AS: 1038756 kB VmallocTotal: 34359738367 kB VmallocUsed: 0 kB VmallocChunk: 0 kB HardwareCorrupted: 0 kB AnonHugePages: 88064 kB CmaTotal: 0 kB CmaFree: 0 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB DirectMap4k: 43008 kB DirectMap2M: 1005568 kB


似乎不按预期工作:https://stackoverflow.com/q/61498709/562769 - Martin Thoma
删除未使用的 import os - davidvandebunte

13

这是我一段时间前整理的东西,它仅适用于Windows,但可能有助于您完成部分所需内容。

基于: “用于系统可用内存” http://msdn2.microsoft.com/en-us/library/aa455130.aspx

"单个进程信息和Python脚本示例" http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true

注意:WMI接口/进程也可用于执行类似任务。我在这里没有使用它,因为当前方法能够满足我的需求,但如果将来需要扩展或改进它,则可以调查WMI工具是否可用。

Python的WMI:

http://tgolden.sc.sabren.com/python/wmi.html

代码:

'''
Monitor window processes

derived from:
>for sys available mem
http://msdn2.microsoft.com/en-us/library/aa455130.aspx

> individual process information and python script examples
http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true

NOTE: the WMI interface/process is also available for performing similar tasks
        I'm not using it here because the current method covers my needs, but if someday it's needed
        to extend or improve this module, then may want to investigate the WMI tools available.
        WMI for python:
        http://tgolden.sc.sabren.com/python/wmi.html
'''

__revision__ = 3

import win32com.client
from ctypes import *
from ctypes.wintypes import *
import pythoncom
import pywintypes
import datetime


class MEMORYSTATUS(Structure):
    _fields_ = [
                ('dwLength', DWORD),
                ('dwMemoryLoad', DWORD),
                ('dwTotalPhys', DWORD),
                ('dwAvailPhys', DWORD),
                ('dwTotalPageFile', DWORD),
                ('dwAvailPageFile', DWORD),
                ('dwTotalVirtual', DWORD),
                ('dwAvailVirtual', DWORD),
                ]


def winmem():
    x = MEMORYSTATUS() # create the structure
    windll.kernel32.GlobalMemoryStatus(byref(x)) # from cytypes.wintypes
    return x    


class process_stats:
    '''process_stats is able to provide counters of (all?) the items available in perfmon.
    Refer to the self.supported_types keys for the currently supported 'Performance Objects'
    
    To add logging support for other data you can derive the necessary data from perfmon:
    ---------
    perfmon can be run from windows 'run' menu by entering 'perfmon' and enter.
    Clicking on the '+' will open the 'add counters' menu,
    From the 'Add Counters' dialog, the 'Performance object' is the self.support_types key.
    --> Where spaces are removed and symbols are entered as text (Ex. # == Number, % == Percent)
    For the items you wish to log add the proper attribute name in the list in the self.supported_types dictionary,
    keyed by the 'Performance Object' name as mentioned above.
    ---------
    
    NOTE: The 'NETFramework_NETCLRMemory' key does not seem to log dotnet 2.0 properly.
    
    Initially the python implementation was derived from:
    http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true
    '''
    def __init__(self,process_name_list=[],perf_object_list=[],filter_list=[]):
        '''process_names_list == the list of all processes to log (if empty log all)
        perf_object_list == list of process counters to log
        filter_list == list of text to filter
        print_results == boolean, output to stdout
        '''
        pythoncom.CoInitialize() # Needed when run by the same process in a thread
        
        self.process_name_list = process_name_list
        self.perf_object_list = perf_object_list
        self.filter_list = filter_list
        
        self.win32_perf_base = 'Win32_PerfFormattedData_'
        
        # Define new datatypes here!
        self.supported_types = {
                                    'NETFramework_NETCLRMemory':    [
                                                                        'Name',
                                                                        'NumberTotalCommittedBytes',
                                                                        'NumberTotalReservedBytes',
                                                                        'NumberInducedGC',    
                                                                        'NumberGen0Collections',
                                                                        'NumberGen1Collections',
                                                                        'NumberGen2Collections',
                                                                        'PromotedMemoryFromGen0',
                                                                        'PromotedMemoryFromGen1',
                                                                        'PercentTimeInGC',
                                                                        'LargeObjectHeapSize'
                                                                     ],
                                                                     
                                    'PerfProc_Process':              [
                                                                          'Name',
                                                                          'PrivateBytes',
                                                                          'ElapsedTime',
                                                                          'IDProcess',# pid
                                                                          'Caption',
                                                                          'CreatingProcessID',
                                                                          'Description',
                                                                          'IODataBytesPersec',
                                                                          'IODataOperationsPersec',
                                                                          'IOOtherBytesPersec',
                                                                          'IOOtherOperationsPersec',
                                                                          'IOReadBytesPersec',
                                                                          'IOReadOperationsPersec',
                                                                          'IOWriteBytesPersec',
                                                                          'IOWriteOperationsPersec'     
                                                                      ]
                                }
        
    def get_pid_stats(self, pid):
        this_proc_dict = {}
        
        pythoncom.CoInitialize() # Needed when run by the same process in a thread
        if not self.perf_object_list:
            perf_object_list = self.supported_types.keys()
                    
        for counter_type in perf_object_list:
            strComputer = "."
            objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
            objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
        
            query_str = '''Select * from %s%s''' % (self.win32_perf_base,counter_type)
            colItems = objSWbemServices.ExecQuery(query_str) # "Select * from Win32_PerfFormattedData_PerfProc_Process")# changed from Win32_Thread        
        
            if len(colItems) > 0:        
                for objItem in colItems:
                    if hasattr(objItem, 'IDProcess') and pid == objItem.IDProcess:
                        
                            for attribute in self.supported_types[counter_type]:
                                eval_str = 'objItem.%s' % (attribute)
                                this_proc_dict[attribute] = eval(eval_str)
                                
                            this_proc_dict['TimeStamp'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.') + str(datetime.datetime.now().microsecond)[:3]
                            break

        return this_proc_dict      
                      
        
    def get_stats(self):
        '''
        Show process stats for all processes in given list, if none given return all processes   
        If filter list is defined return only the items that match or contained in the list
        Returns a list of result dictionaries
        '''    
        pythoncom.CoInitialize() # Needed when run by the same process in a thread
        proc_results_list = []
        if not self.perf_object_list:
            perf_object_list = self.supported_types.keys()
                    
        for counter_type in perf_object_list:
            strComputer = "."
            objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
            objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
        
            query_str = '''Select * from %s%s''' % (self.win32_perf_base,counter_type)
            colItems = objSWbemServices.ExecQuery(query_str) # "Select * from Win32_PerfFormattedData_PerfProc_Process")# changed from Win32_Thread
       
            try:  
                if len(colItems) > 0:
                    for objItem in colItems:
                        found_flag = False
                        this_proc_dict = {}
                        
                        if not self.process_name_list:
                            found_flag = True
                        else:
                            # Check if process name is in the process name list, allow print if it is
                            for proc_name in self.process_name_list:
                                obj_name = objItem.Name
                                if proc_name.lower() in obj_name.lower(): # will log if contains name
                                    found_flag = True
                                    break
                                
                        if found_flag:
                            for attribute in self.supported_types[counter_type]:
                                eval_str = 'objItem.%s' % (attribute)
                                this_proc_dict[attribute] = eval(eval_str)
                                
                            this_proc_dict['TimeStamp'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.') + str(datetime.datetime.now().microsecond)[:3]
                            proc_results_list.append(this_proc_dict)
                    
            except pywintypes.com_error, err_msg:
                # Ignore and continue (proc_mem_logger calls this function once per second)
                continue
        return proc_results_list     

    
def get_sys_stats():
    ''' Returns a dictionary of the system stats'''
    pythoncom.CoInitialize() # Needed when run by the same process in a thread
    x = winmem()
    
    sys_dict = { 
                    'dwAvailPhys': x.dwAvailPhys,
                    'dwAvailVirtual':x.dwAvailVirtual
                }
    return sys_dict

    
if __name__ == '__main__':
    # This area used for testing only
    sys_dict = get_sys_stats()
    
    stats_processor = process_stats(process_name_list=['process2watch'],perf_object_list=[],filter_list=[])
    proc_results = stats_processor.get_stats()
    
    for result_dict in proc_results:
        print result_dict
        
    import os
    this_pid = os.getpid()
    this_proc_results = stats_processor.get_pid_stats(this_pid)
    
    print 'this proc results:'
    print this_proc_results

使用GlobalMemoryStatusEx而不是GlobalMemoryStatus,因为旧的函数可能会返回错误的值。 - phobie
11
你应该避免使用 from x import * 语句!它们会造成主命名空间混乱,并覆盖其他函数和变量。 - phobie

11

我觉得这些答案是针对Python 2编写的,而且无论如何也没有人提到可用于Python 3的标准resource包。该包提供了命令以获取给定进程(默认情况下为调用Python进程)的资源限制。这与获取整个系统当前资源使用情况并不相同,但它可以解决一些相同的问题,比如“我想确保我只使用X数量的RAM运行此脚本。”


1
需要强调的是,这并不回答原始问题(也不太可能是人们正在寻找的)。不过了解这个软件包还是很有好处的。 - webelo
我喜欢使用资源库来监视我的Python代码的想法,所以我在Debian系统上尝试了getrusuage()函数。无论我启动线程和子进程,ru_maxrss始终返回相同的数字,即使使用_SELF、_CHILDREN或_THREAD选项。_BOTH选项出现错误。其他参数(ixrss、idrss、isrss)都返回零。 - Thor

9

这里有所有的好处:psutil + os 用于获取Unix和Windows兼容性。这使我们能够获得:

  1. CPU
  2. 内存
  3. 磁盘

代码:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接