본문으로 바로가기
 

psutil documentation — psutil 5.7.2 documentation

psutil documentation About psutil (python system and process utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python. It is useful mainly for system moni

psutil.readthedocs.io

모듈 소개

psutil은 파이썬을 위한 실행중인 프로세스 및 시스템 리소스 및 정보 검색을 위한 크로스 플랫폼 라이브러리입니다.

 

지원하는 운영체제

  • Linux

  • Windows

  • macOS

  • FreeBSD, OpenBSD, NetBSD

  • Sun Solaris

  • AIX

 

모듈 설치

pip install psutil

(py37) PS C:\Users\Desktop\systeminfo> pip install psutil
Collecting psutil
  Downloading psutil-5.7.2-cp37-cp37m-win_amd64.whl (242 kB)
     |████████████████████████████████| 242 kB 819 kB/s 
Installing collected packages: psutil
Successfully installed psutil-5.7.2

 

CPU 정보 구하기

CPU 사용률

  • psutil.cpu_times_percent() : user, system, idle, interrupt 형식으로 사용률 반환 (Linux 같은 형식)

  • psutil.cpu_percent() : 시스템 전체 cpu 사용률을 반환

>>> import psutil
>>>
>>> psutil.cpu_percent()
5.4
>>>
>>> psutil.cpu_times_percent()
scputimes(user=1.8, system=2.8, idle=94.5, interrupt=0.6, dpc=0.3)
>>> cpu = psutil.cpu_times_percent() 
>>> cpu.idle  
95.1
>>> 100 - cpu.idle
4.900000000000006
>>> int(100 - cpu.idle)
4

CPU 속도

>>> psutil.cpu_freq()
scpufreq(current=2592.0, min=0.0, max=2592.0) 
>>>
>>> f'{str(psutil.cpu_freq().current/1024)} GHz'  
'2.592 GHz'

CPU 코어

  • psutil.cpu_count() : 논리 프로세서 수 (물리코어 x2)
  • psutil.cpu_count(logical=False) : 물리적인 코어 수
>>> psutil.cpu_count() 
12
>>> psutil.cpu_count(logical=False) 
6

 

메모리 정보 구하기

메모리는 조금 어려운 개념입니다. 

전체 크기와 사용 가능한 크기, 사용률 정도만 알아도 충분할 것입니다.

  • total : 물리 메모리 크기
  • available: 사용 가능한 물리 메모리 크기
  • percent: 사용률
  • used: 구지..
  • free: 구지..
>>> import psutil
>>> 
>>> psutil.virtual_memory()
svmem(total=17095180288, available=8609259520, percent=49.6, used=8485920768, free=8609259520)
>>> mem = psutil.virtual_memory()
>>> mem.total/1024**3
15.921127319335938
>>> total = mem.total/1024**3 
>>> f'{round(total)} GB' 
'16 GB'
>>>
>>> avail = mem.available
>>> avail/1024**3
8.216621398925781
>>> f'{round(avail, 1)} GB' 
'8.2 GB'