From 80f4d171223f551c932367bb842f947bf4bd923a Mon Sep 17 00:00:00 2001 From: "Paul J. Davis" Date: Wed, 7 Jul 2010 21:27:43 -0400 Subject: [PATCH] Added an example reloader config to the examples. Thanks to thomasst on GitHub for the sample. Closes #54 --- THANKS | 3 +- ...icorn.conf.py.sample => example_config.py} | 0 examples/example_gevent_reloader.py | 31 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) rename examples/{gunicorn.conf.py.sample => example_config.py} (100%) create mode 100644 examples/example_gevent_reloader.py diff --git a/THANKS b/THANKS index 80b862d4..3b9fa666 100644 --- a/THANKS +++ b/THANKS @@ -11,4 +11,5 @@ Xavier Grangier Sergey Shepelev Chris Dent Matt Good -Randall Leeds \ No newline at end of file +Randall Leeds +thomasst diff --git a/examples/gunicorn.conf.py.sample b/examples/example_config.py similarity index 100% rename from examples/gunicorn.conf.py.sample rename to examples/example_config.py diff --git a/examples/example_gevent_reloader.py b/examples/example_gevent_reloader.py new file mode 100644 index 00000000..3ffd338b --- /dev/null +++ b/examples/example_gevent_reloader.py @@ -0,0 +1,31 @@ +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) +