import os from os import path import sys import thread import time def init(server): thread.start_new_thread(_reload, (server,)) def _reload(server): import inotifyx fd = inotifyx.init() rootdir = path.dirname(path.abspath(__file__)) for dirpath, _, _ in os.walk(rootdir): inotifyx.add_watch(fd, dirpath, inotifyx.IN_CLOSE_WRITE) while True: events = inotifyx.get_events(fd) for event in events: if event.name == '4913': # https://code.google.com/p/vim/source/browse/src/fileio.c?spec=svn94df797ed6b03d5387b5ed7526f8c65024112822&r=29eb4c2a33ac701bfcd4d2e2bed7864eba876e0e#3767 continue elif event.name.endswith('.py'): print event.name, 'changed, reloading...' os.close(fd) server.stop() os.execv(sys.argv[0], sys.argv) return