From c4c596044f80a7ec87bc8827e56ae55fca703e0b Mon Sep 17 00:00:00 2001 From: Jesus Arias Fisteus Date: Thu, 28 Jul 2011 18:44:29 +0200 Subject: [PATCH] Stabilization of the period of ioloop.PeriodicCallback. 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 | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tornado/ioloop.py b/tornado/ioloop.py index 6198de32e..f273a7847 100644 --- a/tornado/ioloop.py +++ b/tornado/ioloop.py @@ -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): -- 2.47.2