什么是你喜欢的方式得到系统当前状态(当前的CPU,RAM,磁盘空间,等等)。 在蟒蛇?积分*尼克斯和视窗平台。

似乎有一些可能的方式提取,从我的搜索:

  1. 使用图书馆等 PSI (目前似乎没有积极发展,并不支持在多个平台)或类似的东西 pystatgrab (再次没有活动自2007年以来它似乎并不支持Windows)。

  2. 使用平台的特定代码,如使用 os.popen("ps") 或类似的*尼克斯的系统和 MEMORYSTATUSctypes.windll.kernel32 (见 这个食谱上ActiveState)为视窗平台。一个能把蟒蛇类一起,与所有那些代码段。

这并不是说那些方法都是坏的,但是已经有一个良好支持的多平台的方式做同样的事情吗?

有帮助吗?

解决方案

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)版本)。


更新:以下是 psutil 的一些示例用法:

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

其他提示

使用 psutil库。在Ubuntu 18.04上,从1-30-2019开始,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()
py = psutil.Process(pid)
memoryUse = py.memory_info()[0]/2.**30  # memory use in GB...I think
print('memory use:', memoryUse)

它给出了Python脚本的当前内存使用。

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

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

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

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

下面的代码,没有外部库为我工作。我在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'

这是我刚才放在一起的东西,它只是窗户,但可以帮助你获得所需的一部分。

来自: “for sys available mem” 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

http:// monkut。 webfactional.com/blog/archive/2009/1/21/windows-process-memory-logging-python

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

操作系统在管理这些资源的方式上有根本的不同。实际上,它们在核心概念方面存在差异,例如定义什么算作系统,什么算作应用时间。

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

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

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

此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()

我们选择使用通常的信息来源,因为我们可以发现可用内存的瞬间波动,并且感觉查询 meminfo 数据源是有帮助的。这也帮助我们获得了一些预先解析的相关参数。

<强>代码

import os

linux_filepath = '/proc/meminfo'
meminfo = dict((i.split()[0].rstrip(':'), int(i.split()[1]))
               for i in open(linux_filepath).readlines())
meta['memory_total_gb'] = meminfo['MemTotal'] / (2**20)
meta['memory_free_gb'] = meminfo['MemFree'] / (2**20)
meta['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   Mlocked:3652 kB SwapTotal:0 kB SwapFree:0 kB脏: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 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

  • CPU的详细信息使用 psutil 图书馆

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

  • RAM频率(在MHz)使用建立在Linux库 dmidecode 和操纵的输出一个位;).这种命令需要根源的权限,因此供应你的密码。只是复制下的赞扬替换 mypass 与你密码

import os

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

------------------- 输出---------------------------
1600吨/s
未知
1600吨/s
未知的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']

您可以在子进程中使用psutil或psmem 示例代码

import subprocess
cmd =   subprocess.Popen(['sudo','./ps_mem'],stdout=subprocess.PIPE,stderr=subprocess.PIPE) 
out,error = cmd.communicate() 
memory = out.splitlines()

参考 http:// techarena51.com/index.php/how-to-install-python-3-and-flask-on-linux/

https://github.com/Leo-g/python-flask-cmd

基于@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])

我不相信有一个支持良好的多平台库。请记住,Python本身是用C语言编写的,因此任何库都可以根据您的建议,明确地决定运行哪个特定于操作系统的代码片段。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top