你需要一個while-loop
,但它可以通過調用notifier.loop
方法隱含設置:
wm = pyinotify.WatchManager()
mask = pyinotify.IN_CREATE
notifier = pyinotify.Notifier(wm, MyProcessEvent(path))
wdd = wm.add_watch(path, mask, rec=True, auto_add=True)
notifier.loop()
如果你想自己設置while-loop
,你可以從source code開始notifier.loop
:
while 1:
try:
notifier.process_events()
# check_events is blocking
if notifier.check_events():
notifier.read_events()
except KeyboardInterrupt:
# Stop monitoring if sigint is caught (Control-C).
break
notifier.stop()
要刪除特定文件或目錄的手錶,請致電wm.rm_watch
。
文件是否會出現什麼情況預先存在時inotify實例是「接通」?
在調用wm.add_watch
之前沒有生成事件。
如果在我處理事件處理函數時創建文件,會發生什麼情況?
事件在大小爲 /proc/sys/fs/inotify/max_queued_events
的緩衝區中排隊。例如,我的系統上 這個數字是
% cat /proc/sys/fs/inotify/max_queued_events
16384
如果文件系統產生足夠的活動來填補,而 你正在處理先前的事件緩衝,那麼你會得到一個IN_Q_OVERFLOW
事件。
查看blucz's answer中的評論。
所以我唯一需要考慮pyinotify不會找到的文件的時間是程序啓動時,目錄(可能)已經有文件。我將需要一個目錄迭代器函數來手動處理這些,然後介紹到pyinotify實例.... – sadmicrowave 2011-12-19 15:04:52
詳細的答案,非常好! – 2011-12-19 15:07:49
@微波:是的,這是正確的。 – unutbu 2011-12-19 18:00:00