From: Ben Darnell Date: Sat, 6 Oct 2012 05:09:07 +0000 (-0700) Subject: Avoid creating multiple redundant autoreload callbacks. X-Git-Tag: v3.0.0~243 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=50e0a97fb09ef43d7addba396fb181893a5b0b54;p=thirdparty%2Ftornado.git Avoid creating multiple redundant autoreload callbacks. --- diff --git a/tornado/autoreload.py b/tornado/autoreload.py index cdb2f124f..62af0f3b0 100644 --- a/tornado/autoreload.py +++ b/tornado/autoreload.py @@ -74,6 +74,7 @@ import sys import traceback import types import subprocess +import weakref from tornado import ioloop from tornado.log import gen_log @@ -85,6 +86,11 @@ except ImportError: signal = None +_watched_files = set() +_reload_hooks = [] +_reload_attempted = False +_io_loops = weakref.WeakKeyDictionary() + def start(io_loop=None, check_time=500): """Restarts the process automatically when a module is modified. @@ -92,6 +98,11 @@ def start(io_loop=None, check_time=500): so will terminate any pending requests. """ io_loop = io_loop or ioloop.IOLoop.instance() + if io_loop in _io_loops: + return + _io_loops[io_loop] = True + if len(_io_loops) > 1: + gen_log.warning("tornado.autoreload started more than once in the same process") add_reload_hook(functools.partial(_close_all_fds, io_loop)) modify_times = {} callback = functools.partial(_reload_on_update, modify_times) @@ -110,8 +121,6 @@ def wait(): start(io_loop) io_loop.start() -_watched_files = set() - def watch(filename): """Add a file to the watch list. @@ -120,8 +129,6 @@ def watch(filename): """ _watched_files.add(filename) -_reload_hooks = [] - def add_reload_hook(fn): """Add a function to be called before reloading the process. @@ -141,8 +148,6 @@ def _close_all_fds(io_loop): except Exception: pass -_reload_attempted = False - def _reload_on_update(modify_times): if _reload_attempted: diff --git a/website/sphinx/releases/next.rst b/website/sphinx/releases/next.rst index 25aebf3d6..3669a535d 100644 --- a/website/sphinx/releases/next.rst +++ b/website/sphinx/releases/next.rst @@ -141,3 +141,7 @@ In progress * The ``{% apply %}`` directive now works properly with functions that return both unicode strings and byte strings (previously only byte strings were supported). +* Calling `tornado.autoreload.start` (or creating an `Application` with + ``debug=True``) twice on the same `IOLoop` now does nothing (instead of + creating multiple periodic callbacks). Starting autoreload on + more than one `IOLoop` in the same process now logs a warning.