]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Prevent duplicate callbacks when PeriodicCallback is stopped and restarted before... 396/head
authorJustin Rosenthal <justin.rosenthal@gmail.com>
Fri, 4 Nov 2011 06:31:49 +0000 (23:31 -0700)
committerJustin Rosenthal <justin.rosenthal@gmail.com>
Fri, 4 Nov 2011 06:31:49 +0000 (23:31 -0700)
tornado/ioloop.py

index bcf5d521c86a911c89d535098bca960376d1c486..12ef90da02be178b0a9ff1f80d5e15f5e824939e 100644 (file)
@@ -455,6 +455,7 @@ class PeriodicCallback(object):
         self.callback_time = callback_time
         self.io_loop = io_loop or IOLoop.instance()
         self._running = False
+        self._timeout = None
 
     def start(self):
         """Starts the timer."""
@@ -465,6 +466,9 @@ class PeriodicCallback(object):
     def stop(self):
         """Stops the timer."""
         self._running = False
+        if self._timeout is not None:
+            self.io_loop.remove_timeout(self._timeout)
+            self._timeout = None
 
     def _run(self):
         if not self._running: return
@@ -479,7 +483,7 @@ class PeriodicCallback(object):
             current_time = time.time()
             while self._next_timeout <= current_time:
                 self._next_timeout += self.callback_time / 1000.0
-            self.io_loop.add_timeout(self._next_timeout, self._run)
+            self._timeout = self.io_loop.add_timeout(self._next_timeout, self._run)
 
 
 class _EPoll(object):