3

我对 pyinotify 有疑问:未调用process_*()的方法ProcessEvent

编码

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 对其进行了测试。我可能会错过什么?

4

2 回答 2

1

问题是缺少 IN_* 的导入,这必须手动完成。我打电话后发现了

daemon.run()

代替

daemon.start()

从而使脚本在前台运行。

于 2009-09-28T12:50:21.443 回答
1

掩码在 EventCodes.ALL_FLAGS 中定义

 mask = EventsCodes.ALL_FLAGS['IN_DELETE']
 mask = EventsCodes.ALL_FLAGS['ALL_EVENTS']
于 2012-01-08T17:47:04.010 回答