Skip to content

Commit 32ef301

Browse files
authored
Update cpu.py
1 parent 31e63a4 commit 32ef301

1 file changed

Lines changed: 8 additions & 24 deletions

File tree

core/cpu.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
1-
import time
1+
import os
22

3-
class CPUMonitor:
4-
5-
def __init__(self):
6-
self.last_total = 0
7-
self.last_idle = 0
8-
9-
def read(self):
10-
with open("/proc/stat","r") as f:
11-
fields = f.readline().split()
12-
13-
user,nice,system,idle,iowait,irq,softirq,steal = map(int,fields[1:9])
143

15-
idle_all = idle + iowait
16-
total = user+nice+system+idle+iowait+irq+softirq+steal
17-
18-
diff_idle = idle_all - self.last_idle
19-
diff_total = total - self.last_total
20-
21-
self.last_idle = idle_all
22-
self.last_total = total
23-
24-
if diff_total == 0:
25-
return 0
4+
class CPUMonitor:
265

27-
return (1 - diff_idle/diff_total) * 100
6+
def get(self):
7+
try:
8+
load = os.getloadavg()[0]
9+
return min(int(load * 10), 100)
10+
except:
11+
return 25

0 commit comments

Comments
 (0)