
-
6942
at 2009-11-06 22:31:58
by vinces1979@gmail.com
Index: /projects/TGScheduler/trunk/tgscheduler/kronos.py
===================================================================
--- /projects/TGScheduler/trunk/tgscheduler/kronos.py (revision 6940)
+++ /projects/TGScheduler/trunk/tgscheduler/kronos.py (revision 6942)
@@ -44,5 +44,5 @@
"""
-__version__="2.0"
+__version__ = "2.0"
__all__ = [
@@ -82,7 +82,7 @@
class method:
- sequential="sequential"
- forked="forked"
- threaded="threaded"
+ sequential = "sequential"
+ forked = "forked"
+ threaded = "threaded"
class Scheduler:
@@ -90,13 +90,13 @@
def __init__(self):
- self.running=True
+ self.running = True
self.sched = sched.scheduler(time.time, self.__delayfunc)
def __delayfunc(self, delay):
- # This delay function is basically a time.sleep() that is
- # divided up, so that we can check the self.running flag while delaying.
- # there is an additional check in here to ensure that the top item of
- # the queue hasn't changed
- if delay<10:
+ """ This delay function is basically a time.sleep() that is
+ divided up, so that we can check the self.running flag while delaying.
+ there is an additional check in here to ensure that the top item of
+ the queue hasn't changed """
+ if delay < 10:
time.sleep(delay)
else:
@@ -191,5 +191,5 @@
else:
raise ValueError("Invalid processmethod")
- task=TaskClass(taskname, weekdays, timeonday, action, args, kw)
+ task = TaskClass(taskname, weekdays, timeonday, action, args, kw)
if monthdays:
# Select the correct MonthdayTask class.
@@ -203,6 +203,6 @@
else:
raise ValueError("Invalid processmethod")
- task=TaskClass(taskname, monthdays, timeonday, action, args, kw)
- firsttime=task.get_schedule_time(True)
+ task = TaskClass(taskname, monthdays, timeonday, action, args, kw)
+ firsttime = task.get_schedule_time(True)
self.schedule_task_abs(task, firsttime)
return task
@@ -257,5 +257,5 @@
self.sched.cancel(task.event)
- if sys.version_info>=(2,6):
+ if sys.version_info >= (2,6):
# code for sched module of python 2.6+
def _getqueuetoptime(self):
@@ -271,13 +271,13 @@
def _run(self):
- # Low-level run method to do the actual scheduling loop.
+ """ Low-level run method to do the actual scheduling loop. """
while self.running:
try:
self.sched.run()
- except Exception,x:
- print >>sys.stderr, "ERROR DURING SCHEDULER EXECUTION",x
- print >>sys.stderr, "".join(
+ except Exception, err:
+ print >> sys.stderr, "ERROR DURING SCHEDULER EXECUTION", err
+ print >> sys.stderr, "".join(
traceback.format_exception(*sys.exc_info()))
- print >>sys.stderr, "-" * 20
+ print >> sys.stderr, "-" * 20
# queue is empty; sleep a short while before checking again
if self.running:
@@ -290,8 +290,8 @@
def __init__(self, name, action, args, kw):
"""This is an abstract class!"""
- self.name=name
- self.action=action
- self.args=args
- self.kw=kw
+ self.name = name
+ self.action = action
+ self.args = args
+ self.kw = kw
def __call__(self, schedulerref):
@@ -299,6 +299,6 @@
try:
self.execute()
- except Exception,x:
- self.handle_exception(x)
+ except Exception, err:
+ self.handle_exception(err)
self.reschedule(schedulerref())
@@ -314,7 +314,7 @@
def handle_exception(self, exc):
"""Handle any exception that occured during task execution."""
- print >>sys.stderr, "ERROR DURING TASK EXECUTION", exc
- print >>sys.stderr, "".join(traceback.format_exception(*sys.exc_info()))
- print >>sys.stderr, "-" * 20
+ print >> sys.stderr, "ERROR DURING TASK EXECUTION", exc
+ print >> sys.stderr, "".join(traceback.format_exception(*sys.exc_info()))
+ print >> sys.stderr, "-" * 20
@@ -335,5 +335,6 @@
def reschedule(self, scheduler):
"""Reschedule this task according to its interval (in seconds)."""
- scheduler.schedule_task(self, self.interval)
+ if scheduler.running:
+ scheduler.schedule_task(self, self.interval)
@@ -369,6 +370,7 @@
# (The execute method in the concrete Task classes will check
# if the current day is a day on which the task must run).
- abstime = self.get_schedule_time(False)
- scheduler.schedule_task_abs(self, abstime)
+ if scheduler.running:
+ abstime = self.get_schedule_time(False)
+ scheduler.schedule_task_abs(self, abstime)
@@ -429,4 +431,5 @@
Scheduler.__init__(self)
# we require a lock around the task queue
+ self.thread = None
self._lock = threading.Lock()
@@ -463,10 +466,10 @@
def threadedcall(self):
- # This method is run within its own thread, so we have to
- # do the execute() call and exception handling here.
+ """ This method is run within its own thread, so we have to
+ do the execute() call and exception handling here."""
try:
self.execute()
- except Exception,x:
- self.handle_exception(x)
+ except Exception, err:
+ self.handle_exception(err)
class ThreadedIntervalTask(ThreadedTaskMixin, IntervalTask):
@@ -534,6 +537,6 @@
try:
self.execute()
- except Exception,x:
- self.handle_exception(x)
+ except Exception, err:
+ self.handle_exception(err)
os._exit(0)
else:
@@ -560,11 +563,11 @@
-if __name__=="__main__":
+if __name__ == "__main__":
def testaction(arg):
- print ">>>TASK",arg,"sleeping 3 seconds"
+ print ">>>TASK", arg, "sleeping 3 seconds"
time.sleep(3)
- print "<<<END_TASK",arg
-
- s=ThreadedScheduler()
+ print "<<<END_TASK", arg
+
+ s = ThreadedScheduler()
s.add_interval_task( testaction, "test action 1", 0, 4, method.threaded, ["task 1"], None )
s.start()