我知道pyinotify.Notifier.check_events(self, timeout=None)可能会暂停--但我更喜欢它无限期轮询。可以打断一下吗?
我正在调用Notifier.stop(self),但它似乎不能从check_events中转义。
在下面的示例中,另一个线程调用stopIt(),并调用self.notifier.stop() -但既没有打印"check_events is True“,也没有打印"check_events is False”:
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()发布于 2011-12-30 12:25:22
即使线程并发运行,每个线程也有自己独立的执行流。一个线程不能通过调用它的方法(比如调用stopIt)将命令注入到另一个线程的执行流中。
那么我们还能做什么呢?除了使用超时这一显而易见的选择之外,您还可以使用另一个线程来创建一个虚拟文件,比如说,它会触发一个MyProcessing可以处理的IN_CREATE事件。
我知道MyProcessing(self)知道self,所以它可以设置self.done = True (其中self是threading.Thread实例,而不是MyProcessing实例)。然后,MyProcessing可以删除虚拟文件。
然后你就可以使用
if (not self.done) and self.notifier.check_events():
print "check_events is True"
self.notifier.read_events()在不设置超时的情况下中断检查。
https://stackoverflow.com/questions/8675666
复制相似问题