这是一个奇怪的问题,strace没有给我任何有用的信息。我使用pyinotify 0.9.6递归地监视一个目录,这样我就可以将对它的更改提交到MySQL数据库中。问题是,当我运行pyinotify时(无论是否有守护程序),我可以删除子目录中的文件,但不能删除子目录本身。rmdir退出,状态为0,并且在系统级别上看起来一切正常,但是子目录仍然在那里,只是令人不寒而栗。我可能是愚蠢的,因为我是新的框架,但这里有一个例子,我是如何初始化手表:
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm)
wm.add_watch('/path/to/dir', pyinotify.IN_CLOSE_WRITE, rec=True, auto_add=True,\
proc_fun=CommitFunction() # I can supply this if you think it's relevant
notifier.loop(daemonize=True, callback=None,\
stdout='/path/to/log.log, stderr='/path/to/log.log')因此,在本例中,如果有一个文件'/path/to/dir/file.txt‘,我可以在'file.txt’上运行一个rm,但它会被删除;但是如果有一个子目录'/path/to/dir/subdir‘在'subdir’上运行rm -r,我会干净利落地退出,但实际上不会删除该目录。
此外,我的日志没有被写入,但我非常确定这是我的错误。
编辑:
下面是一个CommitFunction示例:
class CommitFunction(pyinotify.ProcessEvent):
def process_default(self, event):
dir = event.path
file = event.name
print "%s is the new file" % os.path.join(dir, file)EDIT2:实际上,我的日志可能没有被写入,因为在提交期间我没有在任何地方调用日志或打印函数。我只是直接写入我的MySQL数据库
EDIT3:好的,开始吧。希望我没有深入研究。下面是我在命令行中尝试的内容:
bash$ cd /var/www
bash$ mkdir subdir
bash$ rmdir subdir
bash$ ls
subdir下面是实际的提交函数:
class CommitFunction(pyinotify.ProcessEvent):
def process_default(self, event):
dir = event.path
file = event.name
dbaccess.commit(dir, file) # dir corresponds to a project name如果您愿意,我可以继续深入研究,但是dbaccess拥有我所有用于提交和查询数据库的函数(没有真正涉及文件系统),而且它还进一步从定义我的表的“模型”文件中提取数据。这是一个flask/uwsgi应用程序,如果有帮助的话
发布于 2015-09-02 02:27:15
使用以下代码:
import pyinotify
import os
class CommitFunction(pyinotify.ProcessEvent):
def process_default(self, event):
dir = event.path
file = event.name
print "%s is the new file" % os.path.join(dir, file)
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm)
wm.add_watch('/tmp/testdir', pyinotify.IN_CLOSE_WRITE, rec=True,
auto_add=True, proc_fun=CommitFunction())
notifier.loop(daemonize=False, callback=None)我可以在目标目录中创建和删除子目录,而不会出现错误:
bash-4.3$ pwd
/tmp/testdir
bash-4.3$ mkdir subdir
bash-4.3$ rm -r subdir
bash-4.3$ ls
bash-4.3$ 作为对上述会话的响应,代码在stdout上记录了以下内容:
/tmp/testdir/subdir is the new file
/tmp/testdir/subdir/ is the new file您能确认上述情况在您的环境中是否表现出您所描述的行为吗?如果没有,你能用一个完整的例子来更新你的问题吗?
发布于 2015-09-02 03:12:41
好的,所以我遇到的问题是,删除一个子目录时,IN_CLOSE_WRITE也会被调用,这反过来会触发一个函数,当通过门户网站上传时,我必须确保“项目”的目录存在。
我仍然很困惑,但是这个问题已经解决了。
https://stackoverflow.com/questions/32338271
复制相似问题