Bootstrap

python watchdog 几种 Observer 原理 (linux 挂载(mount)无法正常监听 )

Watchdog 几种 Observer 类型

  • InotifyObserver
  • PollingObserver
  • WindowsApiObserver
  • FSEventsObserver
  • KqueueObserver

InotifyObserver

Linux系统用的, 观察目录的调度事件,效率比较高。 默认使用

WindowsApiObserver

Windows系统用的, 观察目录的调度事件,效率比较高。 默认使用

PollingObserver

与平台无关,轮询目录以检测文件的更改,效率比较低。 如果上两种方式在Linux 或 windows中有问题,可以使用该方法。

其他:

  • 不在做详细讲解
FSEventsObserver

用于 darwin(苹果2000年开源一款操系统)

KqueueObserver

用于,darwin(苹果2000年开源一款操系统) 和 bsd(伯克利Unix)

官方源码

# 是不是Linux系统
if platform.is_linux():
    try:
        from .inotify import InotifyObserver as Observer
    except UnsupportedLibc:
        from .polling import PollingObserver as Observer
# 判断是不是darwin系统
elif platform.is_darwin():
    # FIXME: catching too broad. Error prone
    try:
        from .fsevents import FSEventsObserver as Observer
    except:
        try:
            from .kqueue import KqueueObserver as Observer
            warnings.warn("Failed to import fsevents. Fall back to kqueue")
        except:
            from .polling import PollingObserver as Observer
            warnings.warn("Failed to import fsevents and kqueue. Fall back to polling.")
# 判断是不是bsd系统
elif platform.is_bsd():
    from .kqueue import KqueueObserver as Observer
# 判断是不是windows系统
elif platform.is_windows():
    # TODO: find a reliable way of checking Windows version and import
    # polling explicitly for Windows XP
    try:
        from .read_directory_changes import WindowsApiObserver as Observer
    except:
        from .polling import PollingObserver as Observer
        warnings.warn("Failed to import read_directory_changes. Fall back to polling.")

else:
    from .polling import PollingObserver as Observer

注意

在Linux系统中,如果无法监听到文件目录的变化可以手动的把Observer修改为PollingObserver
Linux系统中的,挂载的目录无法正常监听可以使用下列方法

代码如下:

import sys
import time
import logging
# from watchdog.observers import Observer
from watchdog.observers.polling import PollingObserver
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = LoggingEventHandler()
    observer = PollingObserver()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
;