From: Ben Darnell Date: Sun, 14 Aug 2011 17:38:52 +0000 (-0700) Subject: Add the option to run application hooks before autoreloading. X-Git-Tag: v2.1.0~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=15ebe7641955b081cae9b1a3a9c8b03c5621ef09;p=thirdparty%2Ftornado.git Add the option to run application hooks before autoreloading. Closes #331 --- diff --git a/tornado/autoreload.py b/tornado/autoreload.py index a312ad3c6..b1d669764 100644 --- a/tornado/autoreload.py +++ b/tornado/autoreload.py @@ -74,6 +74,18 @@ def watch(filename): """ _watched_files.add(filename) +_reload_hooks = [] + +def add_reload_hook(fn): + """Add a function to be called before reloading the process. + + Note that for open file and socket handles it is generally + preferable to set the ``FD_CLOEXEC`` flag (using `fcntl` or + `tornado.platform.auto.set_close_exec`) instead of using a reload + hook to close them. + """ + _reload_hooks.append(fn) + _reload_attempted = False def _reload_on_update(io_loop, modify_times): @@ -116,6 +128,8 @@ def _check_file(io_loop, modify_times, path): os.close(fd) except Exception: pass + for fn in _reload_hooks: + fn() if hasattr(signal, "setitimer"): # Clear the alarm signal set by # ioloop.set_blocking_log_threshold so it doesn't fire