pyinotify: 处理 IN_MODIFY 触发器
我正在尝试监控一个文件夹,想要查看文件的修改情况。考虑使用pyinotify这个工具。问题是,当我使用IN_MODIFY事件来检查文件变化时,如果我通过网络将一个小文件,比如12MB的文件,复制到这个文件夹,系统会触发很多次事件。
我不想处理这么多触发的事件。我只想在文件复制完成后触发一次事件。该怎么做呢?
有没有pyinotify方面的高手可以帮忙?
1 个回答
3
试着把 IN_MODIFY
改成 IN_CLOSE_WRITE
。IN_CLOSE_WRITE
事件是在一个可写的文件被关闭时发生的。这个事件通常只会发生一次,除非正在复制文件的程序选择多次关闭这个文件。
上面的改动可能就足够了,但如果不行的话,这个基础代码 可以帮助你了解什么时候发生了哪些事件。通过它,你应该能找出应该使用哪个事件。
# Example: loops monitoring events forever.
#
import pyinotify
# Instanciate a new WatchManager (will be used to store watches).
wm = pyinotify.WatchManager()
# Associate this WatchManager with a Notifier (will be used to report and
# process events).
notifier = pyinotify.Notifier(wm)
# Add a new watch on /tmp for ALL_EVENTS.
wm.add_watch('/tmp', pyinotify.ALL_EVENTS)
# Loop forever and handle events.
notifier.loop()