From 91076a92e4cf582dd83b2f1f79c438bef5eb6521 Mon Sep 17 00:00:00 2001 From: Christophe Benz Date: Thu, 13 May 2010 22:54:02 +0200 Subject: [PATCH] repeat calls function immediately without waiting for timer --- weboob/scheduler.py | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/weboob/scheduler.py b/weboob/scheduler.py index c8791fb6..813a7973 100644 --- a/weboob/scheduler.py +++ b/weboob/scheduler.py @@ -1,27 +1,28 @@ # -*- coding: utf-8 -*- -""" -Copyright(C) 2010 Romain Bignon +# Copyright(C) 2010 Romain Bignon, Christophe Benz +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, version 3 of the License. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - -""" +import logging from threading import Timer, Event + __all__ = ['Scheduler'] + class IScheduler(object): def schedule(self, interval, function, *args): raise NotImplementedError() @@ -43,12 +44,14 @@ class Scheduler(IScheduler): def schedule(self, interval, function, *args): self.count += 1 + logging.debug('function "%s" will be called in %s seconds' % (function.__name__, interval)) timer = Timer(interval, function, args) timer.start() self.queue[self.count] = timer return self.count def repeat(self, interval, function, *args): + function(*args) return self.schedule(interval, self._repeated_cb, interval, function, args) def run(self): @@ -58,6 +61,6 @@ class Scheduler(IScheduler): def want_stop(self): self.stop_event.set() - def _repeated_cb(self, interval, func, args): - func(*args) - self.repeat(interval, func, *args) + def _repeated_cb(self, interval, function, args): + function(*args) + self.repeat(interval, function, *args)