]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Avoid creating multiple redundant autoreload callbacks.
authorBen Darnell <ben@bendarnell.com>
Sat, 6 Oct 2012 05:09:07 +0000 (22:09 -0700)
committerBen Darnell <ben@bendarnell.com>
Sat, 6 Oct 2012 05:09:07 +0000 (22:09 -0700)
tornado/autoreload.py
website/sphinx/releases/next.rst

index cdb2f124fe6a96bb07e96db6d6242d40f8357f18..62af0f3b05f32fae0be05318d11f43db47279e05 100644 (file)
@@ -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:
index 25aebf3d64923c49ed33fa2e06f176b10c7c823a..3669a535d5dad2b09085fd4b61be4631fa2266f5 100644 (file)
@@ -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.