2008-11-09 9 views
200

파이썬에서 현재 시스템 상태 (현재 CPU, RAM, 여유 디스크 공간 등)를 얻는 가장 좋은 방법은 무엇입니까? * nix 및 Windows 플랫폼의 보너스 포인트. pystatgrab 같은 라이브러리 등 PSI (즉, 현재 활발하게 개발되지 보인다 여러 플랫폼에서 지원되지 않음) 또는 무언가를 사용파이썬에서 현재 CPU 및 RAM 사용법을 얻는 방법?

  1. :

    내 검색에서 해당을 추출하는 몇 가지 방법이있는 것 같습니다 (2007 년 이래로 활동이 없었으며 Windows는 지원되지 않음).

  2. 같은 Windows 플랫폼을위한 (this recipe on ActiveState 참조) ctypes.windll.kernel32os.popen("ps") 또는 유닉스 계열의 시스템에 유사한 MEMORYSTATUS을 사용하는 등 플랫폼을 특정 코드를 사용. 파이썬 클래스를 모든 코드 스 니펫과 함께 넣을 수 있습니다.

그런 방법이 좋지 않지만 이미 잘 지원되는 멀티 플랫폼 방식으로 동일한 작업을 수행하고있는 것은 아닙니다.

+0

나만의 멀티 패턴을 만들 수 있습니다. 동적 가져 오기를 사용하여 m 라이브러리 : "sys.platform == 'win32': import win_sysstatus as sysstatus; else "... –

+0

App Engine에서도 작동하는 것은 멋지다 –

+5

왜 유닉스 관련 답변을 수락 했습니까? @ JonCage의 대답을 바꿀 것을 제안합니다. 커뮤니티가 저와 동의합니다 –

답변

244

The psutil library 당신에게 다양한 플랫폼에 대한 몇 가지 시스템 정보 (CPU/메모리 사용을) 줄 것이다 :

psutil은 ps, top 및 Windows 작업 관리자와 같은 도구에서 제공하는 많은 기능을 구현하여 Python을 사용하여 실행 가능한 프로세스 및 시스템 활용도 (CPU, 메모리)에 대한 정보를 휴대용 방식으로 검색하기위한 인터페이스를 제공하는 모듈입니다.

현재 Linux, Windows, OSX, Sun Solaris, FreeBSD, OpenBSD 및 NetBSD (32 비트 및 64 비트 아키텍처)와 2.6에서 3.5 사이의 Python 버전 (Python 2.4 및 2.5 사용자는 2.1을 사용할 수 있습니다. 3 버전).

+3

이 라이브러리는 멋지고 유용하게 보입니다. – riviera

+23

OSX에서 나를 위해 일해 왔습니다 :'$ pip install psutil'; '>>> import psutil; 'vmem (total = 8589934592L, available = 4073336832L, percent = 52.6, used = 5022085120L, free = 3560255488L, active = 2817949696L)을 반환하는'psutil.cpu_percent()'와'>>> psutil.virtual_memory , 비활성 = 513081344L, 유선 = 1691054080L)' – hobs

+4

psutil 라이브러리가 없으면 어떻게 될까요? – BigBrownBear00

-1

잘 지원되는 다중 플랫폼 라이브러리가 있다고 생각하지 않습니다. 파이썬 자체는 C로 작성되었으므로 위 라이브러리에서 어떤 OS 특정 코드 조각을 실행할지 결정하는 것은 간단합니다.

3

"현재 시스템 상태 (현재 CPU, RAM, 여유 디스크 공간 등)"및 "* nix 및 Windows 플랫폼"은 달성하기 어려운 조합 일 수 있습니다.

운영 체제는 이러한 리소스를 관리하는 방식이 근본적으로 다릅니다. 실제로, 시스템으로 간주되는 것이나 적용 시간으로 간주되는 것을 정의하는 것과 같은 핵심 개념이 다릅니다.

"사용 가능한 디스크 공간"? "디스크 공간"으로 간주되는 것은 무엇입니까? 모든 장치의 모든 파티션? 멀티 부팅 환경에서 외부 파티션은 어떻게됩니까?

나는 이것을 가능하게하는 Windows와 * nix간에 충분한 합의가 있다고 생각하지 않습니다. 실제로 Windows라고하는 다양한 운영 체제 간에는 합의가 이루어지지 않을 수도 있습니다. XP와 Vista 모두에서 작동하는 단일 Windows API가 있습니까?

+4

'df -h'가 Windows 및 * nix에서 "디스크 공간"질문에 응답합니다. – jfs

+3

@JFSebastian : Windows XP에서 'df'가 인식되지 않는 Windows는 무엇입니까? 오류 메시지가 표시됩니다. 무엇이 누락 되었습니까? –

+3

Windows에서도 새 프로그램을 설치할 수 있습니다 – jfs

8

내가 전에한데 모아 놓은 것입니다. 창문 일 뿐이며 필요한 것을 할 수 있습니다.

에서 파생

다음 WMI 인터페이스/공정도 비슷한 작업 을 수행 할 수 있습니다 : http://msdn2.microsoft.com/en-us/library/aa455130.aspx

"개별 프로세스 정보와 파이썬 스크립트 예제" http://www.microsoft.com/technet/scriptcenter/scripts/default.mspx?mfr=true

참고 "MEM 사용할 수 SYS은" 현재의 방법이 내 요구 사항을 다루기 때문에 여기서는 사용하지 않지만 언젠가 이것을 확장하거나 향상시켜야한다면 WMI 도구를 조사해야 할 수도 있습니다.파이썬에 대한

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

+0

링크가 끊어졌습니다 –

+0

예, 링크가 b입니다. 지금 roken. –

+0

이전 값이 잘못된 값을 반환 할 수 있기 때문에 GlobalMemoryStatus 대신 GlobalMemoryStatusEx를 사용하십시오. – phobie

24

사용 psutil library와 psutil 또는 PSMEM를 사용할 수 있습니다. 우분투에서 pip는 0.4.3을 설치했습니다. Python에서

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

을 수행하여 psutil 버전을 확인할 수 있습니다.

은 일부 메모리 및 CPU 통계를 얻으려면 :

from __future__ import print_function 
import psutil 
print(psutil.cpu_percent()) 
print(psutil.virtual_memory()) # physical memory usage 

가 나는 또한 할 싶은 : 파이썬 스크립트의 현재 메모리 사용을 제공

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) 

합니다.

pypi page for 4.3.00.5.0에는 몇 가지 심층적 인 예가 있습니다.

우분투 16과 14의 경우 pip에서 phymem_usage() 메소드가없는 버전 4.3.0을 설치했습니다. 외부 라이브러리가 나를 위해 일하지 않고 0.5.0, download the tar.gz file를 얻으려면 다음 코드 아래

tar -xvzf psutil-0.5.0.tar.gz‌​ 
cd psutil-0.5.0 
sudo python setup.py install 
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) 

및 사용, 총, 사용 및 무료 램

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

에만 다음 stdlib 종속성이있는 RAM 사용에 대한 한 - 라이너 :

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