Skip to content

Instantly share code, notes, and snippets.

@quiver
Created March 23, 2013 08:24
Show Gist options
  • Save quiver/5226986 to your computer and use it in GitHub Desktop.
Save quiver/5226986 to your computer and use it in GitHub Desktop.
# 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