2 # -*- coding: iso-8859-15 -*-
9 logger = logging.getLogger(__name__)
16 self.__lock_delay = threading.Lock()
19 logger.debug("Current number of threads: %d" % threading.active_count())
20 self.add_thread(10, self.autoclean)
21 for t in self.lThreads:
23 self.lThreads.remove(t)
26 for t in self.lThreads:
29 def add_thread(self, time, function, args=[], kwargs={}):
32 k = [function, None, args, kwargs]
33 t = threading.Timer(time, self.dispatch, k)
35 t.setName(str(function))
39 self.lThreads.append(t)
41 def dispatch(self, function, timer, args, kwargs):
43 with self.__lock_delay:
45 for t in self.lThreads:
46 if t.name == str(function) and t.isAlive() and t.ident != timer.ident and not t.delayed:
52 logger.debug("Delaying execution of Thread %s", t.name)
53 # Another Thread still running, delay execution
56 function(*args, **kwargs)
58 logger.exception("Dispatcher exception of %s:", t.name)
59 logger.error("Current number of threads: %d" % threading.active_count())
62 pScheduler = Scheduler()
64 if __name__ == "__main__":
65 logging.basicConfig(level=logging.DEBUG)
67 pScheduler.add_thread(0.1, test)