From: Justin Rosenthal Date: Fri, 4 Nov 2011 06:31:49 +0000 (-0700) Subject: Prevent duplicate callbacks when PeriodicCallback is stopped and restarted before... X-Git-Tag: v2.2.0~77^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=700f5a705410811887427c2c5e8fc6f403f1bfeb;p=thirdparty%2Ftornado.git Prevent duplicate callbacks when PeriodicCallback is stopped and restarted before _next_timeout --- 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):