我被卡在/ sys /中,其中包含我的诺基亚N900手机上的环境光线传感器的强度中的光强度。如何轮询/ sys中的文件
See thread on talk.maemo.org here
我试图用pyinotify中轮询文件,但这个看起来某种我错了,因为该文件常是“process_IN_OPEN”,“process_IN_ACCESS”和“process_IN_CLOSE_NOWRITE”
我基本上要得到尽快改变,如果事情发生了转变触发一个事件,执行一类...
这是我试过的代码,它的工作原理,但并不如我所料(我希望的process_IN_MODIFY被触发):
#!/usr/bin/env python
import os, time, pyinotify
import pyinotify
ambient_sensor = '/sys/class/i2c-adapter/i2c-2/2-0029/lux'
wm = pyinotify.WatchManager() # Watch Manager
mask = pyinotify.ALL_EVENTS
def action(self, the_event):
value = open(the_event.pathname, 'r').read().strip()
return value
class EventHandler(pyinotify.ProcessEvent):
...
def process_IN_MODIFY(self, event):
print "MODIFY event:", action(self, event)
...
#log.setLevel(10)
notifier = pyinotify.ThreadedNotifier(wm, EventHandler())
notifier.start()
wdd = wm.add_watch(ambient_sensor, mask)
wdd
time.sleep(5)
notifier.stop()
更新1:
Mmmh,所有我想出了无需线索,如果有一个特殊的机制如下:
f = open('/sys/class/i2c-adapter/i2c-2/2-0029/lux')
while True:
value = f.read()
print value
f.seek(0)
这裹在自己的线程,可以诀窍,但没有人有更聪明,更少的CPU占用和更快的方式来获得最新的价值?