Created
March 23, 2013 08:24
-
-
Save quiver/5226986 to your computer and use it in GitHub Desktop.
Slimmed-down tornado's autoreload
http://www.tornadoweb.org/documentation/autoreload.html http://siguniang.wordpress.com/2012/08/25/tornado-autoreload/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim: set fileencoding=utf8 | |
from tornado import ioloop | |
import functools | |
import os | |
import sys | |
def start(io_loop, check_time=500): | |
modify_times = {} # watch list | |
callback = functools.partial(_reload_on_update, modify_times) | |
scheduler = ioloop.PeriodicCallback(callback, check_time, io_loop=io_loop) | |
scheduler.start() | |
def wait(): | |
io_loop = ioloop.IOLoop() | |
start(io_loop) | |
io_loop.start() | |
def _reload_on_update(modify_times): | |
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] | |
_check_file(modify_times, path) | |
def _check_file(modify_times, path): | |
try: | |
modified = os.stat(path).st_mtime | |
except Exception: | |
return | |
if path not in modify_times: | |
# 起動後1回目の呼び出し | |
modify_times[path] = modified | |
return | |
if modify_times[path] != modified: | |
modify_times[path] = modified | |
on_modified(path) | |
def on_modified(path): | |
# ここにファイル変更時の処理を実装 | |
print "%s modified"%path | |
if __name__ == '__main__': | |
wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment