Python pyinotify已处理事件的返回值

Python pyinotify已处理事件的返回值,python,pyinotify,Python,Pyinotify,我试图从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 "Openi

我试图从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。我做错了什么?

问题出在你的处理器上。让我们看一下代码,我将在重要行中添加注释:

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
因此,您可以在\u OPEN方法中的process\u范围内定义新变量。如果要引用EventHandler实例属性,则需要添加self:

self.endGame = True

@papelucho,如果答案有帮助,请将您的问题标记为已回答,这样可以帮助遇到相同问题的其他人。