repeat calls function immediately without waiting for timer

This commit is contained in:
Christophe Benz 2010-05-13 22:54:02 +02:00
commit 91076a92e4

View file

@ -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)