Python學習筆記__13.4章 psutil

來源:互聯網
上載者:User

標籤:程式設計語言   Python   

# 這是學習廖雪峰老師python教程的學習筆記


在Linux下,有許多系統命令可以讓我們時刻監控系統啟動並執行狀態,如ps,top,free等等。要擷取這些系統資訊,Python可以通過subprocess模組調用並擷取結果。但最好的辦法是用psutil這個第三方模組

psutil = process and system utilities,它不僅可以通過一兩行代碼實現系統監控,還可以跨平台使用,支援Linux/UNIX/OSX/Windows等,是系統管理員和營運小夥伴不可或缺的必備模組

1、擷取CPU資訊

    1)擷取CPU的資訊

>>> import psutil

>>> psutil.cpu_count() # CPU邏輯數量

4

>>> psutil.cpu_count(logical=False) # CPU物理核心

2   # 2說明是雙核超執行緒, 4則是4核非超執行緒

    2)統計CPU的使用者/系統/空閑時間

>>> psutil.cpu_times()

scputimes(user=10963.31, nice=0.0, system=5138.67, idle=356102.45)

  1. 實作類別似top命令的CPU使用率

>>> for x in range(10):  #顯示10次

...     psutil.cpu_percent(interval=1, percpu=True) # 顯示間隔為1秒

2、擷取記憶體信息

    1)使用psutil擷取虛擬記憶體和交換記憶體資訊

>>> psutil.virtual_memory()

svmem(total=8589934592, available=2866520064, percent=66.6, used=7201386496, free=216178688, active=3342192640, inactive=2650341376, wired=1208852480)

>>> psutil.swap_memory()

sswap(total=1073741824, used=150732800, free=923009024, percent=14.0, sin=10705981440, sout=40353792)

3、擷取磁碟資訊

    1)通過psutil擷取磁碟分割、磁碟使用率和磁碟IO資訊

>>> psutil.disk_partitions() # 磁碟分割資訊

[sdiskpart(device='/dev/disk1', mountpoint='/', fstype='hfs', opts='rw,local,rootfs,dovolfs,journaled,multilabel')]

>>> psutil.disk_usage('/') # 磁碟使用方式,這裡的/是掛載點

sdiskusage(total=998982549504, used=390880133120, free=607840272384, percent=39.1)

>>> psutil.disk_io_counters() # 磁碟IO

sdiskio(read_count=988513, write_count=274457, read_bytes=14856830464, write_bytes=17509420032, read_time=2228966, write_time=1618405)

4、擷取網路資訊

    1)psutil可以擷取網路介面和網路連接資訊

>>> psutil.net_io_counters() # 擷取網路讀寫位元組/包的個數

snetio(bytes_sent=3885744870, bytes_recv=10357676702, packets_sent=10613069, packets_recv=10423357, errin=0, errout=0, dropin=0, dropout=0)

>>> psutil.net_if_addrs() # 擷取網路介面資訊

>>> psutil.net_if_stats() # 擷取網路介面狀態

>>> psutil.net_connections() # 擷取當前網路連接資訊

5、擷取進程資訊

    1)psutil可以擷取到所有進程的詳細資料

>>> psutil.pids() # 所有進程ID

[3865, 3864, 3863, 3856, 3855, 3853, 3776, ..., 45, 44, 1, 0]

>>> p = psutil.Process(3776) # 擷取指定進程ID=3776,其實就是當前Python互動環境

>>> p.name() # 進程名稱

'python3.6'

>>> p.exe() # 進程exe路徑

'/Users/michael/anaconda3/bin/python3.6'

>>> p.cwd() # 進程工作目錄

'/Users/michael'

>>> p.cmdline() # 進程啟動的命令列

['python3']

>>> p.ppid() # 父進程ID

3765

>>> p.parent() # 父進程

<psutil.Process(pid=3765, name='bash') at 4503144040>

>>> p.children() # 子進程列表

[]

>>> p.status() # 進程狀態

'running'

>>> p.username() # 進程使用者名稱

'michael'

>>> p.create_time() # 進程建立時間

1511052731.120333

>>> p.terminal() # 進程終端

'/dev/ttys002'

>>> p.cpu_times() # 進程使用的CPU時間

pcputimes(user=0.081150144, system=0.053269812, children_user=0.0, children_system=0.0)

>>> p.memory_info() # 進程使用的記憶體

pmem(rss=8310784, vms=2481725440, pfaults=3207, pageins=18)

>>> p.open_files() # 進程開啟的檔案

[]

>>> p.connections() # 進程相關網路連接

[]

>>> p.num_threads() # 進程的線程數量

1

>>> p.threads() # 所有線程資訊

[pthread(id=1, user_time=0.090318, system_time=0.062736)]

>>> p.environ() # 進程環境變數

{'SHELL': '/bin/bash', 'PATH': '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:...', 'PWD': '/Users/michael', 'LANG': 'zh_CN.UTF-8', ...}

>>> p.terminate() # 結束進程

Terminated: 15 <-- 自己把自己結束了

>>> psutil.test() # 類比出ps命令的效果

 

6、擴充文檔

psutil的官網 (https://github.com/giampaolo/psutil)


Python學習筆記__13.4章 psutil

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.