pyinotify.ThreadedNotifier,process_* 未调用

发布于 2024-08-06 04:27:48 字数 1633 浏览 10 评论 0原文

我在使用 pyinotify 时遇到问题: ProcessEvent 的方法 process_*() 未调用

代码

import sys, time, syslog
from pyinotify import WatchManager, Notifier, ThreadedNotifier, ProcessEvent, EventsCodes
from daemon import Daemon


class PTmp(ProcessEvent):
    def process_IN_CREATE(self, event):
        syslog.syslog("creating: " + event.pathname)
    def process_IN_DELETE(self, event):
        syslog.syslog("creating: " + event.pathname)
    def process_default(self, event):
        syslog.syslog("default: " + event.pathname)

class MyDaemon(Daemon):
    def run(self):
        syslog.openlog('archmind',syslog.LOG_PID,syslog.LOG_DAEMON)
        syslog.syslog('daemon started, entering loop')
        wm = WatchManager()
        mask = IN_DELETE | IN_CREATE
        notifier = ThreadedNotifier(wm, PTmp())
        notifier.start()
        wdd = wm.add_watch('/tmp', mask, rec=True)
        while True:
            time.sleep(1)
        wm.rm_watch(wdd.values())
        notifier.stop()
        syslog.syslog('exiting loop')
        syslog.closelog()

if __name__ == "__main__":
    daemon = MyDaemon('/tmp/archmind.pid')
    if len(sys.argv) == 2:
        if 'start' == sys.argv[1]:
            daemon.start()
        elif 'stop' == sys.argv[1]:
            daemon.stop()
        elif 'restart' == sys.argv[1]:
            daemon.restart()
        else:
            print "Unknown command"
            sys.exit(2)
        sys.exit(0)
    else:
        print "usage: %s start|stop|restart" % sys.argv[0]
        sys.exit(2)

其他所有内容均已记录。 inofity 工作正常,我已经用 inotifywait 对其进行了测试。我可能缺少什么?

I have a problem with pyinotify: the methods process_*() of the ProcessEvent are not called

The code

import sys, time, syslog
from pyinotify import WatchManager, Notifier, ThreadedNotifier, ProcessEvent, EventsCodes
from daemon import Daemon


class PTmp(ProcessEvent):
    def process_IN_CREATE(self, event):
        syslog.syslog("creating: " + event.pathname)
    def process_IN_DELETE(self, event):
        syslog.syslog("creating: " + event.pathname)
    def process_default(self, event):
        syslog.syslog("default: " + event.pathname)

class MyDaemon(Daemon):
    def run(self):
        syslog.openlog('archmind',syslog.LOG_PID,syslog.LOG_DAEMON)
        syslog.syslog('daemon started, entering loop')
        wm = WatchManager()
        mask = IN_DELETE | IN_CREATE
        notifier = ThreadedNotifier(wm, PTmp())
        notifier.start()
        wdd = wm.add_watch('/tmp', mask, rec=True)
        while True:
            time.sleep(1)
        wm.rm_watch(wdd.values())
        notifier.stop()
        syslog.syslog('exiting loop')
        syslog.closelog()

if __name__ == "__main__":
    daemon = MyDaemon('/tmp/archmind.pid')
    if len(sys.argv) == 2:
        if 'start' == sys.argv[1]:
            daemon.start()
        elif 'stop' == sys.argv[1]:
            daemon.stop()
        elif 'restart' == sys.argv[1]:
            daemon.restart()
        else:
            print "Unknown command"
            sys.exit(2)
        sys.exit(0)
    else:
        print "usage: %s start|stop|restart" % sys.argv[0]
        sys.exit(2)

Everything else is logged. inofity works correctly, I have tested it with inotifywait. What may I be missing?

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

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

发布评论

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

评论(2

黯淡〆 2024-08-13 04:27:49

问题在于缺少 IN_* 的导入,这必须手动完成。我在调用后发现了这一点,

daemon.run()

而不是

daemon.start()

使脚本在前台运行。

The problem was missing the import of IN_*, which has to be done manually. I've found that out after calling

daemon.run()

instead of

daemon.start()

thus making the script run in foreground.

厌味 2024-08-13 04:27:49

掩码在 EventCodes.ALL_FLAGS 中定义

 mask = EventsCodes.ALL_FLAGS['IN_DELETE']
 mask = EventsCodes.ALL_FLAGS['ALL_EVENTS']

masks are defined in EventCodes.ALL_FLAGS

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