python watchdog模块不能与redhat服务器

时间:2017-01-11 07:26:14

标签: python django python-2.7 mod-wsgi

我们在redhat服务器的apache2下使用django(1.7.5)和mod_wsgi,并尝试使用看门狗来监控文件。

使用python manager.py runserver命令在本地工作正常,而当我将其部署到产品环境中时,不会在wsgi模式下触发事件

# wsgi.py
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

LOGGER.debug("Starting to watch for config file changes.")
fw = FileWatcher()
# filewatcher
path = settings.PROJECT_ROOT
filename = 'config.json'
class ConfigHandler(FileSystemEventHandler):
    def on_modified(self, event):
        if not event.is_directory and event.src_path.endswith(filename):
            LOGGER.debug("The config has changed!, Reloading")

class FileWatcher(object):

    _instance = None
    _watching = False

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            LOGGER.debug("Creating new FileWatcher")
            cls._instance = super(FileWatcher, cls).__new__(cls, *args, **kwargs)

            cls.start_watching()

        return cls._instance

    @classmethod
    def start_watching(cls):
        if not cls._watching:
            LOGGER.debug("Starting to monitor the file: %s",
                        os.path.join(path, filename))
            event_handler = ConfigHandler()
            observer = Observer()
            observer.schedule(event_handler, path=path, recursive=False)
            observer.start()

            cls._watching = True

1 个答案:

答案 0 :(得分:0)

找到路由原因,默认的Observer(inotify.InotifyObserver)在使用旧linux内核的旧redhat服务器上不起作用

http://pythonhosted.org/watchdog/api.html#module-watchdog.observers

中说明
inotify.InotifyObserver Linux 2.6.13+   inotify(7) based observer
polling.PollingObserver Any fallback implementation

因此我将其改为通用

    from watchdog.observers.polling import PollingObserver
    # http://pythonhosted.org/watchdog/api.html#module-watchdog.observers
    observer = PollingObserver()