From: Jesus Arias Fisteus Date: Thu, 28 Jul 2011 16:44:29 +0000 (+0200) Subject: Stabilization of the period of ioloop.PeriodicCallback. X-Git-Tag: v2.1.0~61^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c4c596044f80a7ec87bc8827e56ae55fca703e0b;p=thirdparty%2Ftornado.git 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. --- 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):