我正在尝试从handle方法返回一个值。我是使用pyinotify的新手,代码是:
import pyinotify
import time
wm = pyinotify.WatchManager()
mask = pyinotify.IN_OPEN
class EventHandler(pyinotify.ProcessEvent):
endGame = False
def process_IN_OPEN(self, event):
print "Opening:", event.pathname
endGame = True
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch('./file.json', mask, rec=True)
wm.rm_watch(wdd.values())
while not handler.endGame:
time.sleep(1)
notifier.stop()
print "end game"
但是当我打开file.json时,endGame变量永远不会变为True。我做错了什么?
答案 0 :(得分:0)
问题出在您的处理程序中。让我们看一下代码(我会在重要的行中添加注释):
class EventHandler(pyinotify.ProcessEvent):
endGame = False # Here class attribute "endGame" is declared
def process_IN_OPEN(self, event):
print "Opening:", event.pathname
endGame = True # Here !local variable! is defined process_IN_OPEN
因此,您在process_IN_OPEN
方法的范围内定义新变量。如果要引用EventHandler
实例属性:
self.endGame = True