我正在尝试从handled方法返回一个值。我是一个使用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。我做错了什么?
发布于 2012-08-20 15:56:14
问题出在您的处理程序中。让我们看一下代码(我将向重要的行添加注释):
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:
self.endGame = Truehttps://stackoverflow.com/questions/12031354
复制相似问题