Larry Cai
Larry Cai

Reputation: 59963

python watchdog module doesn't work with django/mod_wsgi under redhat server

We use django (1.7.5) with mod_wsgi under apache2 in redhat server, and try to use watchdog to monitor the files.

It works fine locally using python manager.py runserver command, while the event is not triggerred in wsgi mode when I deploy it into product environment

# 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

Upvotes: 0

Views: 1257

Answers (1)

Larry Cai
Larry Cai

Reputation: 59963

Found the route cause, the default Observer (inotify.InotifyObserver) doesn't work in old redhat server with old linux kernel

It is stated in http://pythonhosted.org/watchdog/api.html#module-watchdog.observers

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

Therefore I changed it to generic one

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

Upvotes: 0

Related Questions