Changeset 3366 for branches/1.0/turbogears/scheduler.py
- Timestamp:
- 07/25/07 10:47:36 (1 year ago)
- Files:
-
- branches/1.0/turbogears/scheduler.py (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.0/turbogears/scheduler.py
r1496 r3366 17 17 There are three Scheduler classes: 18 18 Scheduler ThreadedScheduler ForkedScheduler 19 19 20 20 You usually add new tasks to a scheduler using the add_interval_task or 21 21 add_daytime_task methods, with the appropriate processmethod argument … … 58 58 self.running=True 59 59 self.sched = sched.scheduler(time.time, self.__delayfunc) 60 60 61 61 def __delayfunc(self, delay): 62 62 # This delay function is basically a time.sleep() that is … … 79 79 if endtime > now: 80 80 time.sleep(endtime - now) 81 81 82 82 def _acquire_lock(self): pass 83 83 def _release_lock(self): pass 84 84 85 85 def add_interval_task(self, action, taskname, initialdelay, interval, processmethod, args, kw): 86 86 """Add a new Interval Task to the schedule. A very short initialdelay or one of … … 152 152 task.event = self.sched.enter(delay, 0, task, 153 153 (weakref.ref(self),) ) 154 154 155 155 def schedule_task_abs(self, task, abstime): 156 156 """Low-level method to add a new task to the scheduler for the given absolute time value.""" … … 165 165 task.event = self.sched.enterabs(abstime, 0, task, 166 166 (weakref.ref(self),) ) 167 167 168 168 169 169 def start(self): 170 170 """Start the scheduler.""" 171 171 self._run() 172 172 173 173 def stop(self): 174 174 """Remove all pending tasks and stop the Scheduler.""" 175 175 self.running=False 176 176 self.sched.queue[:]=[] 177 177 178 178 def cancel(self, task): 179 179 self.sched.cancel(task.event) … … 201 201 self.args=args 202 202 self.kw=kw 203 203 204 204 def __call__(self, schedulerref): 205 205 """Execute the task action in the scheduler's thread.""" … … 209 209 self.handle_exception(x) 210 210 self.reschedule(schedulerref()) 211 211 212 212 def reschedule(self, scheduler): 213 213 """This is an abstract class, this method is defined in one of the sub classes!""" 214 214 raise NotImplementedError("you're using the abstract base class 'Task', use a concrete class instead") 215 215 216 216 def execute(self): 217 217 """Execute the actual task.""" 218 218 self.action(*self.args, **self.kw) 219 219 220 220 def handle_exception(self, exc): 221 221 """Handle any exception that occured during task execution.""" … … 230 230 Task.__init__(self, name, action, args, kw) 231 231 self.interval=interval 232 232 233 233 def reschedule(self, scheduler): 234 234 # reschedule this task according to its interval (in seconds). … … 241 241 def __init__(self, timeonday): 242 242 self.timeonday=timeonday 243 243 244 244 def get_schedule_time(self, today): 245 245 """Calculate the time value at which this task is to be scheduled.""" … … 254 254 now[5]=0 # seconds 255 255 return time.mktime(now) 256 256 257 257 def reschedule(self, scheduler): 258 258 # Reschedule this task according to the daytime for the task. … … 274 274 Task.__init__(self, name, action, args, kw) 275 275 self.days=weekdays 276 276 277 277 def execute(self): 278 278 # This is called every day, at the correct time. We only need to … … 281 281 if weekday in self.days: 282 282 self.action(*self.args, **self.kw) 283 283 284 284 class MonthdayTask(DayTaskRescheduler, Task): 285 285 """A task that is called at specific days in a month (1-31), at a fixed time on the day.""" … … 292 292 Task.__init__(self, name, action, args, kw) 293 293 self.days=monthdays 294 294 295 295 def execute(self): 296 296 # This is called every day, at the correct time. We only need to … … 302 302 try: 303 303 import threading 304 304 305 305 class ThreadedScheduler(Scheduler): 306 306 """A Scheduler that runs in its own thread.""" … … 356 356 if hasattr(os,"fork"): 357 357 import signal 358 358 359 359 class ForkedScheduler(Scheduler): 360 360 """A Scheduler that runs in its own forked process.""" … … 430 430 initialdelay=0, processmethod=method.threaded, taskname=None): 431 431 si = _get_scheduler() 432 return si.add_interval_task(action=action, interval=interval, args=args, 432 return si.add_interval_task(action=action, interval=interval, args=args, 433 433 kw=kw, initialdelay=initialdelay, 434 434 processmethod=processmethod, taskname=taskname) 435 435 436 def add_weekday_task(action, weekdays, timeonday, args=None, kw=None, 436 def add_weekday_task(action, weekdays, timeonday, args=None, kw=None, 437 437 processmethod=method.threaded, taskname=None): 438 438 si = _get_scheduler() 439 439 return si.add_daytime_task(action=action, taskname=taskname, 440 weekdays=weekdays, monthdays=None, timeonday=timeonday, 440 weekdays=weekdays, monthdays=None, timeonday=timeonday, 441 441 processmethod=processmethod, args=args, kw=kw) 442 443 444 def add_monthday_task(action, monthdays, timeonday, 445 args=None, kw=None, 442 443 444 def add_monthday_task(action, monthdays, timeonday, 445 args=None, kw=None, 446 446 processmethod=method.threaded, taskname=None): 447 447 si = _get_scheduler() 448 448 return si.add_daytime_task(action=action, taskname=taskname, 449 weekdays=None, monthdays=monthdays, timeonday=timeonday, 449 weekdays=None, monthdays=monthdays, timeonday=timeonday, 450 450 processmethod=processmethod, args=args, kw=kw) 451 451