python 看门狗模块在 redhat 服务器下无法与 django/mod_wsgi 一起使用
python watchdog module doesn't work with django/mod_wsgi under redhat server
我们在redhat服务器的apache2下使用django(1.7.5)和mod_wsgi,并尝试使用watchdog来监控文件。
在本地使用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
找到路由原因,默认的 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()
我们在redhat服务器的apache2下使用django(1.7.5)和mod_wsgi,并尝试使用watchdog来监控文件。
在本地使用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
找到路由原因,默认的 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()