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

Posted

技术标签:

【中文标题】如何在 Python 中获取当前的 CPU 和 RAM 使用情况?【英文标题】:How to get current CPU and RAM usage in Python? 【发布时间】:2010-09-21 12:31:13 【问题描述】:

在 Python 中获取当前系统状态(当前 CPU、RAM、可用磁盘空间等)的首选方式是什么? *nix 和 Windows 平台的奖励积分。

似乎有几种可能的方法可以从我的搜索中提取它:

    使用诸如 PSI 之类的库(目前似乎没有积极开发并且不受多平台支持)或类似 pystatgrab 之类的东西(自 2007 年以来似乎没有任何活动,并且不支持 Windows)。

    使用特定于平台的代码,例如对于 *nix 系统使用 os.popen("ps") 或类似代码,对于 Windows 平台使用 ctypes.windll.kernel32 中的 MEMORYSTATUS(请参阅 this recipe on ActiveState)。可以将 Python 类与所有这些代码 sn-ps 放在一起。

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

【问题讨论】:

您可以使用动态导入来构建自己的多平台库:“if sys.platform == 'win32': import win_sysstatus as sysstatus; else” ... 如果有一些可以在 App Engine 上运行的东西会很酷。 包的年龄重要吗?如果有人第一次就做对了,为什么他们仍然是对的? 【参考方案1】:

我不相信有一个支持良好的多平台库可用。请记住,Python 本身是用 C 语言编写的,因此任何库都只会像您上面建议的那样,对运行哪个特定于操作系统的代码 sn-p 做出明智的决定。

【讨论】:

psutil 可以做到这一点,和库os的几个语句组合【参考方案2】:

“...当前系统状态(当前 CPU、RAM、可用磁盘空间等)”和“*nix 和 Windows 平台”可能很难实现。

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

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

我认为 Windows 和 *nix 之间没有足够明确的共识来使这成为可能。事实上,在称为 Windows 的各种操作系统之间甚至可能没有任何共识。是否有适用于 XP 和 Vista 的单一 Windows API?

【讨论】:

df -h 在 Windows 和 *nix 上回答“磁盘空间”问题。 @J.F.Sebastian:哪个窗口?我收到“df”无法识别...来自 Windows XP Pro 的错误消息。我错过了什么? 您也可以在 Windows 上安装新程序。【参考方案3】:

这是我不久前整理的东西,它只是 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 因为旧的可能返回错误的值。 你应该避免from x import *声明!它们使主命名空间变得混乱并覆盖其他函数和变量。【参考方案4】:

The psutil library 为您提供各种平台上的 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/

【讨论】:

在 OSX 上为我工作:$ pip install psutil; >>> import psutil; psutil.cpu_percent()>>> psutil.virtual_memory() 返回一个不错的 vmem 对象:vmem(total=8589934592L, available=4073336832L, percent=52.6, used=5022085120L, free=3560255488L, active=2817949696L, inactive=513081344L, wired=1691054080L) 没有 psutil 库怎么办? @user1054424 python 中有一个内置库,名为resource。但是,您似乎最多可以使用它来获取单个 python 进程正在使用的内存和/或其子进程。它似乎也不是很准确。快速测试显示 resource 从我的 mac 实用工具中减少了大约 2MB。 @BigBrownBear00 只需检查 psutil 的来源;) @Jon Cage 嗨,Jon,我可以与您核实一下可用内存和可用内存之间的区别吗?我打算使用 psutil.virtual_memory() 来确定我可以将多少数据加载到内存中进行分析。感谢您的帮助!【参考方案5】:

您可以将 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

【讨论】:

这不是如何使用subprocess 库的好例子。就像它的文档说的那样,你应该避免裸露的Popen,而是使用更高级别的函数subprocess.check_outputsubprocess.run。不清楚./ps_mem 在这里是什么。【参考方案6】:

使用psutil library。在 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 脚本的当前内存使用情况。

pypi page for psutil 上有一些更深入的示例。

【讨论】:

请不要调用变量py 我知道现在这不是最佳实践,但 py 不是关键字或类似的东西。除了不是你所说的描述性变量名之外,还有什么理由不要使用py 在许多其他上下文中普遍使用它来表示“与python有关的东西”,例如redis-py.我不会使用两个字母的 py 来表示当前进程。【参考方案7】:

下面的代码,没有为我工作的外部库。我在 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 results
    print("CPU Usage = " + CPU_Pct)

和 Ram 使用量、总计、已使用和免费

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'

【讨论】:

你不认为grepawk 在Python 中处理字符串会更好吗? 个人对awk不太熟悉,下面做了一个awkless版本的cpu使用sn -p。非常方便,谢谢! 说这段代码不使用外部库是不诚实的。事实上,这些对 grep、awk 和 free 的可用性有很强的依赖性。这使得上面的代码不可移植。 OP 声明“*nix 和 Windows 平台的奖励积分。”【参考方案8】:

仅适用于 Linux: 仅依赖 stdlib 的 RAM 使用单线:

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

编辑:指定的解决方案操作系统依赖项

【讨论】:

非常有用!要直接以人类可读的单位获取它:os.popen('free -th').readlines()[-1].split()[1:]。请注意,此行返回一个字符串列表。 python:3.8-slim-buster 没有free 看看here,@MartinThoma。 used_m,free_m 加起来不等于 tot_m。结果也与 htop 不匹配。我误会了什么?【参考方案9】:

我觉得这些答案是为 Python 2 编写的,无论如何,没有人提到可用于 Python 3 的标准 resource 包。它提供了用于获取资源的命令limits给定进程(默认情况下调用 Python 进程)。这与获取整个系统的当前资源使用情况不同,但它可以解决一些相同的问题,例如“我想确保这个脚本只使用 X 多内存。”

【讨论】:

重要的是要强调这不能回答原始问题(并且可能不是人们正在搜索的内容)。不过,很高兴了解这个软件包。【参考方案10】:

我们为此选择使用通常的信息源,因为我们可以发现可用内存的瞬时波动,并且觉得查询 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 缓冲区:15144 kB 缓存:210720 kB SwapCached:0 kB 活动:261476 kB 非活动:128888 kB 活动(匿名):167092 kB 非活动(匿名):20888 kB 活动(文件):94384 kB 非活动(文件):108000 kB 不可撤销:3652 kB 锁定:3652 kB SwapTotal:0 kB SwapFree:0 kB Dirty:0 kB 回写: 0 kB AnonPages:168160 kB 映射:81352 kB Shmem:21060 kB Slab:34492 kB SReclaimable:18044 kB SUnreclaim:16448 kB KernelStack:2672 kB PageTables:8180 kB NFS_Unstable:0 kB 反弹: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 超大页面大小:2048 kB DirectMap4k:43008 kB DirectMap2M:1005568 kB

【讨论】:

似乎没有按预期工作:***.com/q/61498709/562769【参考方案11】:

这个CPU使用脚本:

import os

def get_cpu_load():
    """ Returns a list CPU Loads"""
    result = []
    cmd = "WMIC CPU GET LoadPercentage "
    response = os.popen(cmd + ' 2>&1','r').read().strip().split("\r\n")
    for load in response[1:]:
       result.append(int(load))
    return result

if __name__ == '__main__':
    print get_cpu_load()

【讨论】:

【参考方案12】:

根据@Hrabal 的 cpu 使用代码,这是我使用的:

from subprocess import Popen, PIPE

def get_cpu_usage():
    ''' Get CPU usage on Linux by reading /proc/stat '''

    sub = Popen(('grep', 'cpu', '/proc/stat'), stdout=PIPE, stderr=PIPE)
    top_vals = [int(val) for val in sub.communicate()[0].split('\n')[0].split[1:5]]

    return (top_vals[0] + top_vals[2]) * 100. /(top_vals[0] + top_vals[2] + top_vals[3])

【讨论】:

【参考方案13】:

有关 CPU 的详细信息,请使用 psutil

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

对于 RAM 频率(以 MHz 为单位),请使用内置的 Linux 库 dmidecode 并稍微操作输出 ;)。此命令需要 root 权限,因此也提供您的密码。只需复制以下建议,将 mypass 替换为您的密码

import os os.system("echo mypass | sudo -S dmidecode -t memory | grep 'Clock Speed' | cut -d ':' -f2")

------- 输出 --------------- 1600 公吨/秒 未知 1600 公吨/秒 未知 0

更具体[i for i in os.popen("echo mypass | sudo -S dmidecode -t memory | grep 'Clock Speed' | cut -d ':' -f2").read().split(' ') if i.isdigit()]

-------------- 输出 -------- ----- ['1600', '1600']

【讨论】:

添加更多描述【参考方案14】:

要对您的程序进行逐行内存和时间分析,我建议使用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

注意:测试于

line_profiler 版本 == 3.0.2

memory_profiler 版本 == 0.57.0

psutil 版本 == 5.7.0


编辑:分析器的结果可以使用TAMPPA 包进行解析。使用它,我们可以获得逐行所需的图

【讨论】:

【参考方案15】:

从第一反应中获取反馈并进行小改动

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

【讨论】:

【参考方案16】:

你可以读取 /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)

【讨论】:

这显然是 Linux 特有的。【参考方案17】:

这汇总了所有好东西: 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

【讨论】:

【参考方案18】:

使用 crontab 运行不会打印 pid

设置:*/1 * * * * sh dog.shcrontab -e 中的这一行

import os
import re

CUT_OFF = 90

def get_cpu_load():
    cmd = "ps -Ao user,uid,comm,pid,pcpu --sort=-pcpu | head -n 2 | tail -1"
    response = os.popen(cmd, 'r').read()
    arr = re.findall(r'\S+', response)
    print(arr)
    needKill = float(arr[-1]) > CUT_OFF
    if needKill:
        r = os.popen(f"kill -9 arr[-2]")
        print('kill:', r)

if __name__ == '__main__':
    # Test CPU with 
    # $ stress --cpu 1
    # crontab -e
    # Every 1 min
    # */1 * * * * sh dog.sh
    # ctlr o, ctlr x
    # crontab -l
    print(get_cpu_load())

【讨论】:

【参考方案19】:

通过结合tqdmpsutil,可以实时监控CPU 和RAM。在运行繁重的计算/处理时可能会很方便。

它也可以在 Jupyter 中运行,无需任何代码更改:

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)

这个代码sn-p也是available as a gist。

【讨论】:

以上是关于如何在 Python 中获取当前的 CPU 和 RAM 使用情况?的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中获取当前 CPU、RAM 和磁盘驱动器的使用情况

如何获取当前任务的CPU编号(在内核中)?

《python》用psutil获取电脑CPU内存等参数信息

《python》用psutil获取电脑CPU内存等参数信息

Python 怎样获取当前计算机的 cpu,内存等信息

如何获取当前进程的CPU使用率,内存使用率