回调函数不被称为Python
我正在编写一个应用程序,它必须能够在python中监控文件系统目录的变化,为了做到这一点,我使用MacFSEvents,FSEvents C API上的python包装器,因为出于明显的效率原因,轮询不能作为一种选择,因为这段代码可以递归地监控大量文件,然而,模块确实可以工作,这样的代码会产生预期的结果回调函数不被称为Python,python,macos,callback,fsevents,Python,Macos,Callback,Fsevents,我正在编写一个应用程序,它必须能够在python中监控文件系统目录的变化,为了做到这一点,我使用MacFSEvents,FSEvents C API上的python包装器,因为出于明显的效率原因,轮询不能作为一种选择,因为这段代码可以递归地监控大量文件,然而,模块确实可以工作,这样的代码会产生预期的结果 from fsevents import Observer, Stream def callback(FSEvent, mask=0, cookie=0): """
from fsevents import Observer, Stream
def callback(FSEvent, mask=0, cookie=0):
"""
the callback that gets called when an event occurs
:param FSEvent: file or dir that originated the event
:param mask: a bitMask that specifies the type of the event
:param cookie: contains info to bind a moved_to to a moved_from event
"""
print(FSEvent)
print('\n')
print(mask)
print('\n')
if __name__ == "__main__":
try:
obs = Observer()
obs.start()
str = Stream(callback, "/Users/filipposavi/ass", file_events=True)
obs.schedule(str)
except KeyboardInterrupt:
obs.stop()
obs.join
但是,当我将其放入应该管理(相对)低级文件系统细节的类中时,回调不会被调用,下面您可以找到不起作用的代码
import hashlib
import os
import fsevents
from database import Db
BUFSIZE = 2 ** 3
chunksize = 1024
IN_CREATE = 0x00000100
IN_ATTRIB = 0x00000004
IN_MODIFY = 0x00000002
IN_DELETE = 0x00000200
IN_MOVED_FROM = 0x00000040
IN_MOVED_TO = 0x00000080
class FsCore(object):
def __init__(self):
self.db = Db()
def callback(self, FSEvent, mask, cookie=0):
"""
the callback that gets called when an event occurs
:param FSEvent: file or dir that originated the event
:param mask: a bitMask that specifies the type of the event
:param cookie: contains info to bind a moved_to to a moved_from event
"""
print('ciao')
if (mask & IN_CREATE) == IN_CREATE:
self.on_created(FSEvent)
if (mask & IN_ATTRIB) == IN_ATTRIB:
self.on_attrib(FSEvent)
if (mask & IN_DELETE) == IN_DELETE:
self.on_deleted(FSEvent)
if (mask & IN_MODIFY) == IN_MODIFY:
self.on_modified(FSEvent)
if (mask & IN_MOVED_FROM) == IN_MOVED_FROM:
self.on_moved(FSEvent, cookie, False)
if (mask & IN_MOVED_TO) == IN_MOVED_TO:
def startFSEvents(self, path):
"""
:param path:
"""
self.observer = fsevents.Observer()
self.observer.start()
self.stream = fsevents.Stream(self.callback, path, file_events=True)
self.observer.schedule(self.stream)
def stopFSEvents(self):
"""
"""
self.observer.unschedule(self.stream)
self.observer.join()
self.observer.stop()
下面是触发事件/回调(单元测试的一部分)的代码
希望你们能给我一个正确的方向,因为我对python有点生疏,所以我非常坚持这个方向
self.fs = FsCore()
self.fs.startFSEvents(self.path)
self.database = Db()
path = self.path + "/tmp"
file = open(path, mode='a')
file.write("ciao")
file.flush()
file.close()
if not self.database.getFile(path):
self.fail("onCreated callback doesn't works or it's not called at all")