]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Stabilization of the period of ioloop.PeriodicCallback. 320/head
authorJesus Arias Fisteus <jfisteus@gmail.com>
Thu, 28 Jul 2011 16:44:29 +0000 (18:44 +0200)
committerJesus Arias Fisteus <jfisteus@gmail.com>
Thu, 28 Jul 2011 17:17:00 +0000 (19:17 +0200)
Previously, the timeout for the following event was computed by adding
callback_time to the actual time the callback finished, which caused
calls to the callback not being trully periodic.

This commit makes callback_time be a stable period by scheduling the
next timeout callback_time after the previous timeout was scheduled.

tornado/ioloop.py

index 6198de32e41acf3ec76d2722896971326316c4d2..f273a784740fe01813397b9e300f6da5ec0829f5 100644 (file)
@@ -434,8 +434,8 @@ class PeriodicCallback(object):
     def start(self):
         """Starts the timer."""
         self._running = True
-        timeout = time.time() + self.callback_time / 1000.0
-        self.io_loop.add_timeout(timeout, self._run)
+        self._next_timeout = time.time()
+        self._schedule_next()
 
     def stop(self):
         """Stops the timer."""
@@ -447,8 +447,12 @@ class PeriodicCallback(object):
             self.callback()
         except Exception:
             logging.error("Error in periodic callback", exc_info=True)
+        self._schedule_next()
+
+    def _schedule_next(self):
         if self._running:
-            self.start()
+            self._next_timeout += self.callback_time / 1000.0
+            self.io_loop.add_timeout(self._next_timeout, self._run)
 
 
 class _EPoll(object):