Break pyinotify notifier loop

1.9k Views Asked by At

I'm using pyinotify.notifier to keep track of changes in a text file.

When I get an specific change in it, I want to break the notifier loop. By using notifier.stop() seems to not work.

Here is what I'm trying to do:

class ModHandler(pyinotify.ProcessEvent):
    def process_IN_MODIFY(self, evt):
        #... Do Stuff
        if "Expected change":
               #break notifier loop

if __name__ == "__main__":

    handler = ModHandler()
    wm = pyinotify.WatchManager()
    notifier = pyinotify.Notifier(wm, handler)
    wdd = wm.add_watch('example.file', pyinotify.IN_MODIFY)
    notifier.loop()
    #when finished the loop, do more stuff

How can break the thread loop and return to the main program?

2

There are 2 best solutions below

1
On BEST ANSWER

The documentation states that:

notifier.loop()
The call to this method is blocking until we type c-c (sigint)

So that's what you need to do. Send a sigint signal. Some ways of doing that:

0
On

Since version 0.9.0 you can stop notifier loop by passing a callback function. When evaluated to True, breaks the loop and stops the notifier.

https://github.com/seb-m/pyinotify/wiki/Recent-Developments#changes-introduced-with-pyinotify-090