From 700f5a705410811887427c2c5e8fc6f403f1bfeb Mon Sep 17 00:00:00 2001 From: Justin Rosenthal Date: Thu, 3 Nov 2011 23:31:49 -0700 Subject: [PATCH] Prevent duplicate callbacks when PeriodicCallback is stopped and restarted before _next_timeout --- tornado/ioloop.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tornado/ioloop.py b/tornado/ioloop.py index bcf5d521c..12ef90da0 100644 --- a/tornado/ioloop.py +++ b/tornado/ioloop.py @@ -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): -- 2.47.2