Python守护程序使用pyxs Xenstore clien监视GPIO管脚

2024-09-29 21:44:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pyxspythonxenstore客户机模块编写一个Upstart守护进程,它监视一个设备上GPIO控制器上的一组输出管脚。启动后,守护程序的基本结构是导出相关的pin,为pin添加相应的Xenstore路径,并为每个Xenstore路径添加和监视监视。监视部分是线程化的-对于每一个手表,都会创建一个线程,该线程使用一个目标worker方法来监视监视监视的更改。根据PyXS的文档,你基本上必须做一些事情,比如:

# monitor is a pyxs.client.Client.Monitor object, and watch adds a
# watch to the given path
monitor.watch(path, path_token)
# wait for events on the watched path - returns a pair if there is an
# event, the first is the event path and the second is the path token
monitor.wait(sleep=...)

我的问题是,如果没有指定sleep=<time>参数,那么对wait的调用是否会阻塞-从PyXS文档中不清楚是否是这样。在

代码大致如下:

^{pr2}$

Tags: andthepath文档路径tokeneventis
1条回答
网友
1楼 · 发布于 2024-09-29 21:44:54

来自^{}文档:

wait(sleep=None)

Waits for any of the watched paths to generate an event, which is a (path, token) pair, where the first element is event path, i.e. the actual path that was modified and second element is a token, passed to the watch(). Parameters: sleep (float) – number of seconds to sleep between event checks.

这意味着,sleep参数实际上是分离对wait方法的偶数检查,如果您查看github中的code

def wait(self, sleep=None):
    """Waits for any of the watched paths to generate an event,
    which is a ``(path, token)`` pair, where the first element
    is event path, i.e. the actual path that was modified and
    second element is a token, passed to the :meth:`watch`.
    :param float sleep: number of seconds to sleep between event
                        checks.
    """
    while True:
        if self.client.events:
            packet = self.client.events.popleft()
            return Event(*packet.payload.split("\x00")[:-1])

        # Executing a noop, hopefuly we'll get some events queued
        # in the meantime. Note: I know it sucks, but it seems like
        # there's no other way ...
        self.client.execute_command(Op.DEBUG, "")

        if sleep is not None: #sleep here is to provide tap gap between event check
            time.sleep(sleep)

您将看到,sleep参数只是提供事件检查之间的抽头间隙,因为while True循环一直在运行,直到您取消event,或者如果您想:检查event的速度有多快,sleep则常规检查的速度越快。在

因此,最后:

wait方法的调用已经阻塞,sleep只是为了给event检查之间的时间间隔。在

相关问题 更多 >

    热门问题