Opengist

Explore

  • All gists
  • Topics
  • Users
Give feedback on the new UI Powered by Opengist ⋅ 201ms

user:thomas gists created by user

title:mygist gists with given title

description:sync gists with given description

filename:myfile.txt gists having files with given name

extension:yml gists having files with given extension

language:go gists having files with given language

topic:homelab gists with given topic

all:systemctl search all fields

Login
d

disk

Recently created Least recently created Recently updated Least recently updated

timmy / 使用 psutil 監控系統資源

Last active 6 hours ago cpu disk memory network process psutil python system-monitoring

psutil 模組可用於獲取 CPU、記憶體、磁碟、網路、進程等系統資源資訊,適用於系統監控、效能分析與資源管理工具。

0 0 0 1
1 import psutil
2
3 # 獲取 CPU 使用率
4 cpu_usage = psutil.cpu_percent(interval=1)
5 print(f"CPU 使用率: {cpu_usage}%")
6
7 # 獲取記憶體資訊
8 memory_info = psutil.virtual_memory()
9 print(f"總記憶體: {memory_info.total / (1024**3):.2f} GB")
10 print(f"可用記憶體: {memory_info.available / (1024**3):.2f} GB")