python获取系统信息模块psutil
生活随笔
收集整理的這篇文章主要介紹了
python获取系统信息模块psutil
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
psutil,(process and system utilities),可以通過一兩行代碼實現系統監控,還可以跨平臺使用,支持Linux/UNIX/OSX/Windows等,是系統管理員和運維小伙伴不可或缺的必備模塊。
1 安裝
pip install psutil2 獲取CPU信息
In [2]: psutil.cpu_count() # CPU邏輯數量 Out[2]: 8In [3]: psutil.cpu_count(logical=False) # CPU物理核心 Out[3]: 4In [4]: psutil.cpu_times() # 統計CPU的用戶/系統/空閑時間 Out[4]: scputimes(user=830519.73, nice=0.0, system=723038.84, idle=42315894.17)- 實現類似top命令的CPU使用率,每秒刷新一次,累計10次 In [6]: for x in range(10):...: r = psutil.cpu_percent(interval=1, percpu=True)...: print(r)...: [19.0, 0.0, 12.7, 1.0, 8.0, 1.0, 4.0, 1.0] [20.0, 0.0, 12.1, 0.0, 8.0, 0.0, 4.0, 1.0] [32.0, 5.0, 23.0, 4.0, 19.0, 4.0, 16.0, 2.0] [20.0, 0.0, 15.0, 0.0, 7.1, 0.0, 5.9, 2.0] [24.8, 1.0, 14.9, 1.0, 9.0, 1.0, 8.1, 0.0] [34.0, 4.0, 27.0, 4.0, 24.8, 4.0, 17.8, 5.0] [28.7, 2.0, 20.0, 1.0, 13.0, 2.0, 11.0, 1.0] [21.0, 1.0, 15.8, 1.0, 10.9, 0.0, 6.9, 1.0] [22.0, 0.0, 14.9, 0.0, 9.0, 0.0, 7.0, 0.0] [25.0, 2.0, 15.2, 1.0, 11.0, 1.0, 5.0, 0.0]3 獲取內存信息
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' In [7]: psutil.virtual_memory() # 獲取物理內存信息 Out[7]: svmem(total=17179869184, available=5688819712, percent=66.9, used=9728593920, free=159133696, active=5531738112, inactive=5306654720, wired=4196855808)In [8]: psutil.swap_memory() # 獲取交換內存信息 Out[8]: sswap(total=5368709120, used=4316463104, free=1052246016, percent=80.4, sin=659438723072, sout=4277862400) - 注意: 返回的是字節為單位的整數。 總內存17179869184/1024/1024/1024 = 16.0 G。4 獲取磁盤信息
In [9]: psutil.disk_partitions() # 磁盤分區信息 Out[9]: [sdiskpart(device='/dev/disk1s1', mountpoint='/', fstype='apfs', opts='rw,local,rootfs,dovolfs,journaled,multilabel'),sdiskpart(device='/dev/disk1s4', mountpoint='/private/var/vm', fstype='apfs', opts='rw,noexec,local,dovolfs,dontbrowse,journaled,multilabel,noatime')] - 有兩個分區,磁盤格式apfs,opts中journaled表示支持日志。In [10]: psutil.disk_usage('/') # 磁盤使用情況 Out[10]: sdiskusage(total=250685575168, used=118098038784, free=125440405504, percent=48.5) - 磁盤總共250685575168/1024/1024/1024233.469G,已使用48.5%。In [11]: psutil.disk_io_counters() # 磁盤IO Out[11]: sdiskio(read_count=55081907, write_count=65301593, read_bytes=996595785728, write_bytes=1063386177536, read_time=18258582, write_time=10621452)5 獲取網絡信息
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:531509025 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' In [12]: psutil.net_io_counters() # 獲取網絡讀寫字節/包的個數 Out[12]: snetio(bytes_sent=80803235840, bytes_recv=124806110208, packets_sent=254195219, packets_recv=182450714, errin=0, errout=130782, dropin=0, dropout=0)In [13]: psutil.net_if_addrs() # 獲取網絡接口信息In [14]: psutil.net_if_stats() # 獲取網絡接口狀態In [15]: psutil.net_connections() # 獲取當前網絡連接信息6 獲取進程信息
In [16]: psutil.pids() # 所有進程ID Out[16]: [0,1,39,40, ...]In [17]: import osIn [18]: os.getpid() Out[18]: 70042In [19]: p = psutil.Process(os.getpid()) # 獲取指定進程In [20]: p.name() # 進程名稱 Out[20]: 'python3.7'In [21]: p.exe() # 進程exe路徑 Out[21]: '/opt/anaconda3/bin/python3.7'In [22]: p.cwd() # 進程工作目錄 Out[22]: '/Users/liuyouyuan/workspace/pyproject/scripts'In [23]: p.cmdline() # 進程啟動的命令行 Out[23]: ['/opt/anaconda3/bin/python', '/opt/anaconda3/bin/ipython']In [24]: p.ppid() # 父進程ID Out[24]: 46810In [25]: p.parent() # 父進程 Out[25]: psutil.Process(pid=46810, name='zsh', started='2020-03-30 15:24:07')In [26]: p.children() # 子進程列表 Out[26]: []In [27]: p.status() # 進程狀態 Out[27]: 'running'In [28]: p.username() # 進程用戶名 Out[28]: 'liuyouyuan'In [29]: p.create_time() # 進程創建時間 Out[29]: 1586241277.044256In [30]: p.terminal() # 進程終端 Out[30]: '/dev/ttys003'In [31]: p.cpu_times() # 進程使用的CPU時間 Out[31]: pcputimes(user=3.119619328, system=0.63911072, children_user=0.0, children_system=0.0)In [32]: p.memory_info() # 進程使用的內存 Out[32]: pmem(rss=73498624, vms=4479959040, pfaults=28482, pageins=185)In [33]: p.open_files() # 進程打開的文件 Out[33]: [popenfile(path='/Applications/Visual Studio Code.app/Contents/Resources/electron.asar', fd=26),popenfile(path='/Applications/Visual Studio Code.app/Contents/Resources/app/node_modules.asar', fd=28),popenfile(path='/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Resources/English.lproj/Localized.rsrc', fd=33)]In [34]: p.connections() # 進程相關網絡連接 Out[34]: []In [35]: p.num_threads() # 進程的線程數量 Out[35]: 6In [36]: p.threads() # 所有線程信息 Out[36]: [pthread(id=1, user_time=3.008698, system_time=0.4743),pthread(id=2, user_time=0.008189, system_time=0.028232),pthread(id=3, user_time=3.7e-05, system_time=2.4e-05),pthread(id=4, user_time=3.8e-05, system_time=2.5e-05),pthread(id=5, user_time=3.3e-05, system_time=1.6e-05),pthread(id=6, user_time=2e-05, system_time=1.7e-05)]In [37]: p.environ() # 進程環境變量 Out[37]: {'TMPDIR': '/var/folders/ys/hf5c1mt91qgdq3r803d6qdq80000gn/T/','__CF_USER_TEXT_ENCODING': '0x1F5:0x19:0x34','SHELL': '/bin/zsh',...'_': '/opt/anaconda3/bin/ipython'}In [38]: p.terminate() # 結束進程 [1] 70042 terminated ipython (base)總結
以上是生活随笔為你收集整理的python获取系统信息模块psutil的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python内置函数zip map fi
- 下一篇: python3函数中lambda/fil