Domanda

Ho un problema con pyinotify: la modalità process_*() del ProcessEvent non sono chiamati

Il codice

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)

Tutto il resto viene registrato. inofity funziona correttamente, ho provato con inotifywait. Cosa posso mancare?

È stato utile?

Soluzione

Il problema mancava l'importazione di IN_ *, che deve essere fatto manualmente. Ho scoperto che fuori dopo aver chiamato

daemon.run()

anziché

daemon.start()

rendendo così lo script eseguito in primo piano.

Altri suggerimenti

maschere sono definiti in EventCodes.ALL_FLAGS

 mask = EventsCodes.ALL_FLAGS['IN_DELETE']
 mask = EventsCodes.ALL_FLAGS['ALL_EVENTS']
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top