Reputation: 3651
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()
Upvotes: 1
Views: 1223
Reputation: 880777
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.
Upvotes: 1