[英]pyinotify return value from handled event
I'm trying to return a value from handled method. 我正在尝试从已处理方法返回一个值。 I'm very newbie using pyinotify, the code is:
我是使用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"
But when I open file.json, the endGame variable never turns to True. 但是,当我打开file.json时,endGame变量永远不会变为True。 What am I doing wrong?
我究竟做错了什么?
The problem is in your handler. 问题出在您的处理程序中。 Let's look at the code (I'll add comments to significant lines):
让我们看一下代码(我将在重要的行中添加注释):
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
So, you define new variable in the scope of process_IN_OPEN
method. 因此,您可以在
process_IN_OPEN
方法的范围内定义新变量。 You need to add self if you want to refer to EventHandler
instance attribute: 如果要引用
EventHandler
实例属性,则需要添加self:
self.endGame = True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.