码迷,mamicode.com
首页 > 编程语言 > 详细

第一个python实例--监控cpu

时间:2015-11-18 12:43:34      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:

 1 #第一个python实例:监控cpu
 2 #/bin/bash/env Python
 3 from __future__ import print_function
 4 from collections import OrderedDict
 5 import pprint
 6 
 7 def CPUinfo():
 8     ‘‘‘ Return the information in /proc/CPUinfo
 9     as a dictionary in the following format:
10     CPU_info[‘proc0‘]={...}
11     CPU_info[‘proc1‘]={...}
12     ‘‘‘
13     CPUinfo=OrderedDict()
14     procinfo=OrderedDict()
15 
16     nprocs = 0
17     with open(/proc/cpuinfo) as f:
18         for line in f:
19             if not line.strip():
20                 # end of one processor
21                 CPUinfo[proc%s % nprocs] = procinfo
22                 nprocs=nprocs+1
23                 # Reset
24                 procinfo=OrderedDict()
25             else:
26                 if len(line.split(:)) == 2:
27                     procinfo[line.split(:)[0].strip()] = line.split(:)[1].strip()
28                 else:
29                     procinfo[line.split(:)[0].strip()] = ‘‘
30 
31     return CPUinfo
32 
33 if __name__==__main__:
34     CPUinfo = CPUinfo()
35     for processor in CPUinfo.keys():
36         print(CPUinfo[processor][model name])

 

第一个python实例--监控cpu

标签:

原文地址:http://www.cnblogs.com/itor/p/4974023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!