六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 52|回复: 0

写了一个根据cpu使用率 发声的小程序.

[复制链接]

升级  44%

30

主题

30

主题

30

主题

秀才

Rank: 2

积分
116
 楼主| 发表于 2013-1-15 02:29:45 | 显示全部楼层 |阅读模式
下午烦躁,躺着睡觉
想起来前天看黑客帝国,第6分50秒,neo的电脑发出一种beep,觉得很酷
想弄在一把
 
于是写了个代码:
它会 根据cpu使用率 发声
cpu usage           2秒内发声情况
10%                    . (2声)
20%                    ..(4声)
...
100%                    ....................(20声)
 
于是乎,如果cpu使用率接近100%的话,她就会不停的发声:)
 
 
 

#!/usr/bin/env python2.6#coding=utf8#<<neo>>  06:45-6:55 beep ...  ... .....#if the cpu usage is full ,the beep will not stop. # usage:#    sudo modprobe pcspkr#    sudo apt-get install beepimport osimport re, timedef beep(frequency, duration=100, repeat=10, delay=10): #100 ms    cmd = 'beep -f %s -l %s -r %s -d %s' % (frequency,duration, repeat, delay)    print cmd    os.system(cmd)last_all = 0last_used = 0def cpu_usage():    str = file('/proc/stat').read()    arr = re.split(r'\s+', str)[1:5]    print arr    arr = [int(item) for item in arr]    print arr    cpu_all = sum(arr)    cpu_used = sum(arr[0:3])    usage = 1.0 * (cpu_used- last_used) / (cpu_all - last_all)    global last_used, last_all    last_all = cpu_all    last_used = cpu_used    print usage    return usagefor i in range (0, 100000):    usage = cpu_usage()    t1 = time.time()    beep(80, 10, int(usage * 20)+1, 100) #beep for (usage*10+10) times, will tack 1s-2s    t2 = time.time()    print t1, t2    time.sleep(2-(t2-t1))  #2s sub the time used in beep 
 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表