如何轮询 /sys 中的文件

发布于 2024-09-04 06:39:12 字数 1330 浏览 2 评论 0原文

我一直在读取 /sys/ 中的一个文件,其中包含诺基亚 N900 手机上环境光传感器的光强度(以勒克斯为单位)。

在此处查看 talk.maemo.org 上的帖子

我尝试使用 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:

嗯,我想出的所有内容都没有任何线索。特殊的机制如下:

f = open('/sys/class/i2c-adapter/i2c-2/2-0029/lux')
while True:
    value = f.read()
    print value
    f.seek(0)

这个包裹在自己的线程中,可以解决问题,但是有没有人有更聪明、更少占用 CPU 和更快的方法来获取最新值?

I am stuck reading a file in /sys/ which contains the light intensity in Lux of the ambient light sensor on my Nokia N900 phone.

See thread on talk.maemo.org here

I tried to use pyinotify to poll the file but this looks some kind of wrong to me since the file is alway "process_IN_OPEN", "process_IN_ACCESS" and "process_IN_CLOSE_NOWRITE"

I basically want to get the changes ASAP and if something changed trigger an event, execute a class...

Here's the code I tried, which works, but not as I expected (I was hoping for process_IN_MODIFY to be triggered):

#!/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()

Update 1:

Mmmh, all I came up without having a clue if there is a special mechanism is the following:

f = open('/sys/class/i2c-adapter/i2c-2/2-0029/lux')
while True:
    value = f.read()
    print value
    f.seek(0)

This, wrapped in a own thread, could to the trick, but does anyone have a smarter, less CPU-hogging and faster way to get the latest value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋千易 2024-09-11 06:39:12

由于 /sys/file 是一个伪文件,它仅呈现底层、易失性操作系统值的视图,因此永远不会引发修改事件是有道理的。由于该文件是从下面“修改”的,因此它不遵循常规文件系统语义。

如果从未引发修改事件,那么使用像 pinotify 这样的包不会有任何帮助。 '最好寻找特定于平台的机制。

对更新 1 的响应

由于 N900 maemo 运行时支持 GFileMonitor,您最好检查它是否可以提供您想要的异步事件。

据我所知,忙碌的等待是一种浪费。在手机上,它确实会耗尽电池。你至少应该在忙碌的循环中睡觉。

Since the /sys/file is a pseudo-file which just presents a view on an underlying, volatile operating system value, it makes sense that there would never be a modify event raised. Since the file is "modified" from below it doesn't follow regular file-system semantics.

If a modify event is never raised, using a package like pinotify isn't going to get you anywhere. 'twould be better to look for a platform-specific mechanism.

Response to Update 1:

Since the N900 maemo runtime supports GFileMonitor, you'd do well to check if it can provide the asynchronous event that you desire.

Busy waiting - as I gather you know - is wasteful. On a phone it can really drain a battery. You should at least sleep in your busy loop.

指尖上得阳光 2024-09-11 06:39:12

嗯,我在不知道是否有特殊机制的情况下想到了以下内容:

f = open('/sys/class/i2c-adapter/i2c-2/2-0029/lux')
while True:
    value = f.read()
    print value
    f.seek(0)

这个包裹在自己的线程中,可以解决问题,但是有没有人有更聪明、更少占用 CPU 和更快的方法来获得最新值?

干杯
比约恩

Mmmh, all I came up without having a clue if there is a special mechanism is the following:

f = open('/sys/class/i2c-adapter/i2c-2/2-0029/lux')
while True:
    value = f.read()
    print value
    f.seek(0)

This, wrapped in a own thread, could to the trick, but does anyone have a smarter, less CPU-hogging and faster way to get the latest value?

Cheers
Bjoern

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文