From ba8d0921bbf1c309110c9e90ffe90c3ab37ef311 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Wed, 14 Jul 2010 17:11:19 +0200 Subject: [PATCH] use mutex to lock scheduler --- weboob/core/scheduler.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/weboob/core/scheduler.py b/weboob/core/scheduler.py index 9dff6011..c1749085 100644 --- a/weboob/core/scheduler.py +++ b/weboob/core/scheduler.py @@ -17,7 +17,7 @@ import logging -from threading import Timer, Event +from threading import Timer, Event, RLock __all__ = ['Scheduler'] @@ -38,6 +38,7 @@ class IScheduler(object): class Scheduler(IScheduler): def __init__(self): + self.mutex = RLock() self.stop_event = Event() self.count = 0 self.queue = {} @@ -46,15 +47,17 @@ class Scheduler(IScheduler): if self.stop_event.isSet(): return - self.count += 1 - logging.debug('function "%s" will be called in %s seconds' % (function.__name__, interval)) - timer = Timer(interval, self._callback, (self.count, function, args)) - self.queue[self.count] = timer - timer.start() - return self.count + with self.mutex: + self.count += 1 + logging.debug('function "%s" will be called in %s seconds' % (function.__name__, interval)) + timer = Timer(interval, self._callback, (self.count, function, args)) + self.queue[self.count] = timer + timer.start() + return self.count def _callback(self, count, function, args): - self.queue.pop(count) + with self.mutex: + self.queue.pop(count) return function(*args) def repeat(self, interval, function, *args): @@ -65,9 +68,10 @@ class Scheduler(IScheduler): def _wait_to_stop(self): self.want_stop() - for e in self.queue.itervalues(): - e.cancel() - e.join() + with self.mutex: + for e in self.queue.itervalues(): + e.cancel() + e.join() def run(self): try: