代码之家  ›  专栏  ›  技术社区  ›  lpfavreau Arthur Debert

如何在Python中获得当前的CPU和RAM使用率?

  •  246
  • lpfavreau Arthur Debert  · 技术社区  · 16 年前

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

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

    1. 使用诸如 PSI (目前似乎没有积极开发,在多平台上也不受支持)或类似的东西 pystatgrab (自2007年以来似乎再也没有活动,也不支持Windows)。

    2. os.popen("ps") 或类似的*nix系统和 MEMORYSTATUS 在里面 ctypes.windll.kernel32 this recipe on ActiveState )对于Windows平台。可以将Python类与所有这些代码片段放在一起。

    12 回复  |  直到 12 年前
        1
  •  522
  •   Community vonPryz    4 年前

    The psutil library 为您提供各种平台上的CPU、RAM等信息:

    psutil是一个模块,提供了一个接口,通过使用Python以可移植的方式检索有关运行进程和系统利用率(CPU、内存)的信息,实现了ps、top和Windows task manager等工具提供的许多功能。


    一些例子:

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

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

        2
  •  82
  •   wordsforthewise    3 年前

    使用 psutil library . 在Ubuntu 18.04上,pip从2019年1月30日起安装了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 .

        3
  •  48
  •   Hrabal    5 年前

    仅适用于Linux:

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

        4
  •  24
  •   eyllanesc RAHUL KUMAR    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'
    
        5
  •  14
  •   Pe Dro    4 年前

    逐行 memory_profiler line_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 注:测试日期为

    测线剖面仪 版本==3.0.2

    内存分析器 版本==0.57.0

    psutil 版本==5.7.0


    编辑:可以使用 TAMPPA 包裹使用它,我们可以得到一行一行想要的图 plot

        6
  •  13
  •   monkut    3 年前

    这是我不久前整理的东西,它只是windows,但可能会帮助您完成部分需要完成的工作。

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

    http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true

    注意: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
    
        7
  •  9
  •   Martin Thoma    4 年前

    我们选择使用通常的信息源,因为我们可以在空闲内存中找到瞬时的波动,并感觉到对数据的查询 内存信息 数据来源很有用。这也帮助我们获得了一些预先解析的相关参数。

    密码

    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交换缓存:0 kB活动:261476 kB M锁定:3652 kB交换总计:0 kB交换自由:0 kB脏:0 kB写回: 0 kB anonpage:168160 kB映射:81352 kB Shmem:21060 kB Slab:34492 可申请知识库:18044KB SunRecreal:16448KB内核堆栈:2672KB 页表:8180 kB NFS\u不稳定:0 kB跳出:0 kB写回TMP:0 kB 34359738367 kB VMAlloccused:0 kB VmallocChunk:0 kB硬件已损坏: 0 kB无注释页:88064 kB总:0 kB免费:0 kB HugePages\u总计:0 HugePages\u免费:0 HugePages\u Rsvd:0 HugePages\u剩余: 0 Hugepagesize:2048 kB DirectMap4k:43008 kB DirectMap2M:1005568 kB

        8
  •  8
  •   anoneemus    6 年前

    我觉得这些答案是为Python2编写的,而且在任何情况下都没有人提到标准 resource 可用于Python3的包。它提供获取资源的命令 给定进程(默认情况下是调用Python进程)的。这与获取当前值不同 用法 但它可以解决一些相同的问题,例如“我想确保我在这个脚本中只使用X多的RAM。”

        9
  •  8
  •   Rea Haas    4 年前

    这是所有好东西的总和: psutil + os 获取Unix&Windows兼容性: 这使我们能够:

    1. 记忆力
    2. 磁盘

    代码:

    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
    
        10
  •  5
  •   S.Lott    16 年前

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

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

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

        11
  •  5
  •   tripleee    3 年前

    从第一次响应中获得反馈,并做了一些小改动

    #!/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
    
        12
  •  4
  •   Subhash    6 年前

    此脚本用于检查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()
    
        13
  •  3
  •   Saptarshi Ghosh    5 年前
    • psutil 图书馆

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

    • 对于RAM频率(以MHz为单位),请使用内置的Linux库 并稍微操纵输出;)。此命令需要root权限,因此也需要提供密码。只需复制以下内容即可 我的通行证 用你的密码

    import os

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

    -------------------输出---------------------------
    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
  •  3
  •   Leroy Kayanda    4 年前

    您可以读取/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)
    
        15
  •  2
  •   Karol Zlot Mickael    3 年前

    通过组合,可以实现实时CPU和RAM监控 tqdm psutil . 当运行繁重的计算/处理时,它可能很方便。

    enter image description here

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

    enter image description here

    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)
    

    available as a gist .

        16
  •  1
  •   Jay    6 年前

    根据@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])
    
        17
  •  1
  •   Leyo R    3 年前

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

        18
  •  0
  •   CodeFarmer    3 年前

    使用crontab运行不会打印pid

    设置: */1 * * * * sh dog.sh 这条线 crontab -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
  •  -12
  •   user35952    16 年前

    我不相信有一个支持良好的多平台库可用。请记住,Python本身是用C语言编写的,所以任何库都只需做出一个明智的决定,即运行哪个操作系统特定的代码段,正如您上面所建议的那样。