idning 发表于 2013-1-15 02:29:45

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

下午烦躁,躺着睡觉
想起来前天看黑客帝国,第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)    print arr    arr =     print arr    cpu_all = sum(arr)    cpu_used = sum(arr)    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 
 
页: [1]
查看完整版本: 写了一个根据cpu使用率 发声的小程序.