def get_windows_cpu_speed():
import winreg
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"HARDWARE\DESCRIPTION\System\CentralProcessor\0")
speed, type = winreg.QueryValueEx(key, "~MHz")
speed = round(float(speed)/1024, 1)
return "{speed} GHz".format(speed=speed)
用psutil包
cpu:
>>> import psutil>>> psutil.cpu_times()
scputimes(user=3961.46, nice=169.729, system=2150.659, idle=16900.540, iowait=629.59, irq=0.0, softirq=19.42, steal=0.0, guest=0, nice=0.0)
>>>
>>> for x in range(3):
... psutil.cpu_percent(interval=1)
...
4.0
5.9
3.8
>>>
>>> for x in range(3):
... psutil.cpu_percent(interval=1, percpu=True)
...
[4.0, 6.9, 3.7, 9.2]
[7.0, 8.5, 2.4, 2.1]
[1.2, 9.0, 9.9, 7.2]
>>>
>>>
>>> for x in range(3):
... psutil.cpu_times_percent(interval=1, percpu=False)
...
scputimes(user=1.5, nice=0.0, system=0.5, idle=96.5, iowait=1.5, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
scputimes(user=1.0, nice=0.0, system=0.0, idle=99.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
scputimes(user=2.0, nice=0.0, system=0.0, idle=98.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
>>>
>>> psutil.cpu_count()
4
>>> psutil.cpu_count(logical=False)
2
>>>
内存:
>>> psutil.virtual_memory()svmem(total=8374149120L, available=2081050624L, percent=75.1, used=8074080256L, free=300068864L, active=3294920704, inactive=1361616896, buffers=529895424L, cached=1251086336)
>>> psutil.swap_memory()
sswap(total=2097147904L, used=296128512L, free=1801019392L, percent=14.1, sin=304193536, sout=677842944)
>>>
python 获取代码 :12
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
32
33
34
#!/usr/bin/python
# -*- coding:utf8 -*-
__author__ = 'chenwx'
def cpu_rate():
import time
def cpu_r():
f = open("/proc/stat","r")
for f_line in f:
break
f.close()
f_line = f_line.split(" ")
f_line_a=[]
for i in f_line:
if i.isdigit():
i=int(i)
f_line_a.append(i)
total = sum(f_line_a)
idle = f_line_a[3]
return total,idle
total_a,idle_a=cpu_r()
time.sleep(2)
total_b,idle_b=cpu_r()
sys_idle = idle_b - idle_a
sys_total = total_b - total_a
sys_us = sys_total - sys_idle
cpu_a = (float(sys_us)/sys_total)*100
return cpu_a
# print cpu_rate()