1

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?

MSO
  • 409
  • 1
  • 9
  • 22

2 Answers2

3

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:

Adelin
  • 7,809
  • 5
  • 37
  • 65
  • It worked. I've add `os.kill(os.getpid(),signal.SIGINT)` and it finished the loop. Thanks for your help. – MSO Dec 11 '17 at 12:50
1

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

Matias Gonzalez
  • 2,930
  • 2
  • 15
  • 8