2 # -*- coding: iso-8859-15 -*-
9 logger = logging.getLogger(__name__)
18 logger.debug("Current number of threads: %d" % threading.active_count())
19 self.add_thread(10, self.autoclean)
20 for t in self.lThreads:
22 self.lThreads.remove(t)
25 for t in self.lThreads:
28 def add_thread(self, time, function, args=[], kwargs={}):
31 k = [function, None, args, kwargs]
32 t = threading.Timer(time, self.dispatch, k)
34 t.setName(str(function))
37 self.lThreads.append(t)
39 def dispatch(self, function, timer, args, kwargs):
43 for t in self.lThreads:
44 if t.name == str(function) and t.isAlive() and t.ident != timer.ident:
49 logger.debug("Delaying execution of Thread %s", t.name)
50 # Another Thread still running, delay execution
53 function(*args, **kwargs)
55 logger.exception("Dispatcher exception of %s:", t.name)
56 logger.error("Current number of threads: %d" % threading.active_count())
59 pScheduler = Scheduler()
61 if __name__ == "__main__":
62 logging.basicConfig(level=logging.DEBUG)
64 pScheduler.add_thread(0.1, test)