forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.py
More file actions
31 lines (28 loc) · 904 Bytes
/
Copy pathcpu.py
File metadata and controls
31 lines (28 loc) · 904 Bytes
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
import psutil
import time
import sys
# Get per-core and total CPU usage (%)
def get_cpu_usage():
return {
'per_core': psutil.cpu_percent(interval=0.1, percpu=True),
'total': psutil.cpu_percent(interval=0.1)
}
# Get CPU temperature (if available)
def get_cpu_temperature():
try:
temps = psutil.sensors_temperatures()
for name, entries in temps.items():
for entry in entries:
if 'cpu' in entry.label.lower() or 'core' in entry.label.lower():
return entry.current
return None
except (AttributeError, NotImplementedError):
return None
# Collect CPU usage history for the last N seconds (1 value/sec)
def get_cpu_usage_history(seconds=60):
history = []
for _ in range(seconds):
usage = psutil.cpu_percent()
history.append(usage)
time.sleep(1)
return history