>  기사  >  백엔드 개발  >  Python의 psutil 모듈을 사용하는 방법

Python의 psutil 모듈을 사용하는 방법

WBOY
WBOY앞으로
2023-05-17 13:52:062014검색

    1. psutil 모듈:

    1.psutil 소개

    psutil은 실행 중인 시스템의 프로세스와 시스템 활용도를 쉽게 얻을 수 있는 크로스 플랫폼 라이브러리(//pythonhosted.org/psutil/)입니다. (CPU, 메모리, 디스크, 네트워크 등 포함) 정보. 주로 시스템 모니터링, 성능 분석, 프로세스 관리에 사용됩니다. ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iostat, iotop, uptime, pidof, tty, tasket, pmap 등과 같은 동등한 명령줄 도구에서 제공하는 기능을 구현합니다. . 현재 Linux, Windows, OS 등 32비트 및 64비트 운영 체제를 지원합니다. CPU 정보

    cp_times 메소드를 사용하면 아래와 같이 CPU의 전체 정보를 얻을 수 있습니다.

    CentOS安装psutil包:
    python版本:3.5
    wget https://pypi.python.org/packages/source/p/psutil/psutil-3.2.1.tar.gz --no-check-certificate
    tar zxvf psutil-3.2.1.tar.gz
    cd psutil-3.2.1
    python setup.py install
    Windos安装psutil包:
    D:\python35\Scripts>pip3.exe install psutil
    Collecting psutil
      Downloading psutil-5.3.1-cp35-cp35m-win_amd64.whl (215kB)
        100% |████████████████████████████████| 225kB 84kB/s
    Installing collected packages: psutil
    Successfully installed psutil-5.3.1

    아래와 같이 사용자의 CPU 시간 또는 IO 대기 시간과 같은 단일 데이터를 가져옵니다.
    >>> psutil.cpu_times()
    scputimes(user=650613.02, nice=22.14, system=154916.5, idle=16702285.26, iowait=68894.55, irq=3.38, softirq=7075.65, steal=0.0, guest=0.0)
    >>>
    CPU의 논리적 및 물리적 개수를 가져옵니다. 기본 논리 값은 True입니다.

    >>> psutil.cpu_times().user
    650617.11
    >>> psutil.cpu_times().iowait
    68894.63
    >>>

    CPU 사용량 가져오기:

    #CPU逻辑个数
    >>> psutil.cpu_count()
    2
    #CPU物理个数
    >>> psutil.cpu_count(logical=False)
    1
    >>>

    2. 메모리 정보

    virtual_memory 메서드는 주로 메모리 정보를 가져오는 데 사용됩니다. 스왑을 사용하려면 swap_memory 메서드를 사용하세요.

    >>> psutil.cpu_percent()
    2.5
    >>> psutil.cpu_percent(1)
    2.5
    >>>

    백분율은 실제 사용된 메모리 비율, 즉 (1047543808-717537280)/1047543808*100%를 나타냅니다. available은 아직 사용할 수 있는 메모리를 나타냅니다.

    3. 디스크 정보

    디스크 정보에는 두 가지 주요 부분이 있습니다. 하나는 디스크 사용률이고 다른 하나는 io입니다. 각각 disk_usage 및 disk_io_counters 방법을 통해 얻을 수 있습니다.

    먼저 파티션 정보를 가져온 다음 루트 파티션의 사용법을 확인하세요.

    >>> mem = psutil.virtual_memory()
    >>> mem
    svmem(total=4018601984, available=1066205184, percent=73.5, used=3904004096, free=114597888, active=3302174720, inactive=426078208, buffers=156520448, cached=795086848)
    >>> mem.total
    4018601984
    >>> mem.used
    3904004096
    >>> mem.free
    114597888
    >>> print(mem.total/1024/1024)
    3832.4375
    >>>

    기본 disk_io_counters 방법은 하드 디스크의 총 IO 수와 읽기 및 쓰기 정보를 가져오고 IO를 가져와서 읽고 써야 하는 경우입니다. 단일 파티션의 정보를 기록하려면 " perdisk=True" 매개변수를 추가하세요.
    >>> psutil.disk_partitions()
    [sdiskpart(device='/dev/mapper/root', mountpoint='/', fstype='ext4', opts='rw,errors=remount-ro'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='ext2', opts='rw')]
    >>> psutil.disk_usage('/')
    sdiskusage(total=42273669120, used=17241096192, free=22885195776, percent=40.8)
    >>>

    4. 네트워크 정보:

    네트워크 io와 디스크 io의 사용 방법은 주로 net_io_counters 방법이 사용됩니다. 단일 네트워크 카드의 io 정보를 얻으려면 pernic=True를 추가하세요. 매개변수.

    >>> psutil.disk_io_counters()
    sdiskio(read_count=638190, write_count=77080153, read_bytes=16037795840, write_bytes=1628871606272, read_time=2307367, write_time=1777841305)
    >>> psutil.disk_io_counters(perdisk=True)
    {'vdb1': sdiskio(read_count=312, write_count=0, read_bytes=1238016, write_bytes=0, read_time=95, write_time=0), 'vda1': sdiskio(read_count=637878, write_count=77080257, read_bytes=16036557824, write_bytes=1628873314304, read_time=2307272, write_time=1777841879)}
    >>>

    5. 기타 시스템 정보:

    1. 부팅 시간 가져오기

    #获取网络总的io情况
    >>> 
    >>> psutil.net_io_counters()
    snetio(bytes_sent=525490132009, bytes_recv=409145642892, packets_sent=948527563, packets_recv=778182181, errin=0, errout=0, dropin=0, dropout=0)
    #获取网卡的io情况
    >>> 
    >>> psutil.net_io_counters(pernic=True)
    {'lo': snetio(bytes_sent=56524704027, bytes_recv=56524704027, packets_sent=33602236, packets_recv=33602236, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=468966480940, bytes_recv=352622081327, packets_sent=914930488, packets_recv=744583332, errin=0, errout=0, dropin=0, dropout=0)}
    >>>

    2. 모든 시스템 프로세스 보기

    ##以linux时间格式返回,可以使用时间戳转换
    >>> psutil.boot_time()    
    1496647567.0
    #转换成自然时间格式
    >>> psutil.boot_time()
    1496647567.0
    >>> datetime.datetime.fromtimestamp(psutil.boot_time ()).strftime("%Y-%m-%d %H: %M: %S")
    '2017-06-05 15: 26: 07'
    >>>

    시스템 하드웨어 스크립트 보기: 정보 스크립트

    >>> psutil.pids()
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 46, 47, 48, 49, 50, 51, 52, 53, 60, 61, 63, 64, 65, 97, 98, 279, 280, 331, 398, 481, 676, 693, 769, 845, 848, 1023, 1085, 1108, 1355, 1366, 1457, 1474, 1475, 1494, 1541, 1543, 1545, 1546, 1548, 1550, 1552, 2829, 12436, 12913, 13129, 16022, 16029, 16030, 16031, 16032, 16033, 16518, 16520, 17088, 17124, 19203, 25382, 32679]

    위 내용은 Python의 psutil 모듈을 사용하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

    성명:
    이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제