gunicorn/examples/example_gevent_reloader.py
Paul J. Davis 80f4d17122 Added an example reloader config to the examples.
Thanks to thomasst on GitHub for the sample.

Closes #54
2010-07-07 21:27:43 -04:00

32 lines
962 B
Python

import gevent
import logging
import os
import signal
import sys
def when_ready(server):
def monitor():
modify_times = {}
while True:
for module in sys.modules.values():
path = getattr(module, "__file__", None)
if not path: continue
if path.endswith(".pyc") or path.endswith(".pyo"):
path = path[:-1]
try:
modified = os.stat(path).st_mtime
except:
continue
if path not in modify_times:
modify_times[path] = modified
continue
if modify_times[path] != modified:
logging.info("%s modified; restarting server", path)
os.kill(os.getpid(), signal.SIGHUP)
modify_times = {}
break
gevent.sleep(1)
gevent.Greenlet.spawn(monitor)