Question

I know pyinotify.Notifier.check_events(self, timeout=None) can take a timeout - but I'd prefer it poll indefinitely. Is it possible to interrupt it?

I am calling Notifier.stop(self), but it does not seem to be escaping from check_events.

In the below example, another thread calls stopIt(), and self.notifier.stop() is called - but neither "check_events is True" nor "check_events is False" is printed:

def run(self):
    self.notifier = pyinotify.Notifier(self.monitor, MyProcessing(self))
    while True:
        self.notifier.process_events()
        print "Waiting at check_events"
        if self.notifier.check_events():
            print "check_events is True"
            self.notifier.read_events()
        else:
            print "check_events is False"
    print "Out of while"
    return True

def stopIt(self):
    self.notifier.stop()
Was it helpful?

Solution

Even though threads run concurrently, each thread has its own independent flow of execution. One thread can not inject commands into another thread's flow of execution by calling its methods (such as by calling stopIt).

So what else can we do? Well, besides the obvious option of using the timeout, you could use the other thread to create a dummy file, say, which triggers an IN_CREATE event which MyProcessing could then process.

I see MyProcessing(self) knows self, so it could set self.done = True, (where self is the threading.Thread instance, not the MyProcessing instance.) MyProcessing could then delete the dummy file.

Then you could use

    if (not self.done) and self.notifier.check_events():
        print "check_events is True"
        self.notifier.read_events()

to break out of the check without setting a timeout.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top