debuggers.hg

view tools/python/xen/xend/scheduler.py @ 4655:a838a908e38e

bitkeeper revision 1.1327.2.2 (4267a9b3MhPpljnjQ5IbfLdzcW2K3w)

Remove twisted from the HTTP server and replace with a
threaded server. Add classes to provide tcp and unix servers
using threads instead of twisted. Remove use of twisted from
the consoles, event server and HTTP resources

Signed-off-by: Mike Wray <mike.wray@hp.com>
author mjw@wray-m-3.hpl.hp.com
date Thu Apr 21 13:25:07 2005 +0000 (2005-04-21)
parents c69fbe48a357
children 16efdf7bbd57
line source
1 import threading
3 class Scheduler:
5 def __init__(self):
6 self.lock = threading.Lock()
7 self.schedule = {}
9 def later(self, _delay, _name, _fn, args, kwargs={}):
10 """Schedule a function to be called later (if not already scheduled).
12 @param _delay: delay in seconds
13 @param _name: schedule name
14 @param _fn: function
15 @param args: arguments (list)
16 @param kwargs keyword arguments (map)
17 """
18 try:
19 self.lock.acquire()
20 if self.schedule.get(_name): return
21 runargs = [ _name, _fn, args, kwargs ]
22 timer = threading.Timer(_delay, self._run, args=runargs)
23 self.schedule[_name] = timer
24 finally:
25 self.lock.release()
26 timer.start()
28 def cancel(self, name):
29 """Cancel a scheduled function call.
31 @param name: schedule name to cancel
32 """
33 timer = self._remove(name)
34 if timer:
35 timer.cancel()
37 def _remove(self, name):
38 try:
39 self.lock.acquire()
40 timer = self.schedule.get(name)
41 if timer:
42 del self.schedule[name]
43 return timer
44 finally:
45 self.lock.release()
47 def _run(self, name, fn, args, kwargs):
48 self._remove(name)
49 fn(*args, **kwargs)