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.
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."""
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):