Hello tg people, here is a small patch (against the 1.0 branch trunk)
that allows one to schedule one-time tasks via the
turbogears.scheduler.add_onetime_task function. It's basically the
same as interval_task but doesn't reschedule. I found this to be
useful for starting long running processes when the client is not
required to wait for the result.
BTW, is this list the right place for submitting patches?
Cheers,
Daniel
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears Trunk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears-trunk?hl=en
-~----------~----~----~----~------~----~------~--~---
Index: scheduler.py
===================================================================
--- scheduler.py (revision 4084)
+++ scheduler.py (working copy)
@@ -24,6 +24,7 @@
For more control you could use one of the following Task classes
and use schedule_task or schedule_task_abs:
IntervalTask ThreadedIntervalTask ForkedIntervalTask
+ OnetimeTask ThreadedOnetimeTask ForkedOnetimeTask
WeekdayTask ThreadedWeekdayTask ForkedWeekdayTask
MonthdayTask ThreadedMonthdayTask ForkedMonthdayTask
@@ -105,6 +106,27 @@
self.schedule_task(task, initialdelay)
return task
+ def add_onetime_task( self, action, taskname, initialdelay, processmethod, args, kw ):
+ """Add a new task to the scheduler that will only be executed once."""
+ if initialdelay<0:
+ raise ValueError("delay must be >0")
+ # Select the correct OnetimeTask class. Not all types may be available!
+ if processmethod==method.sequential:
+ TaskClass=OnetimeTask
+ elif processmethod==method.threaded:
+ TaskClass = ThreadedOnetimeTask
+ elif processmethod==method.forked:
+ TaskClass = ForkedOnetimeTask
+ else:
+ raise ValueError("invalid processmethod")
+ if not args:
+ args=[]
+ if not kw:
+ kw={}
+ task = TaskClass(taskname, action, args, kw)
+ self.schedule_task(task, initialdelay)
+ return task
+
def add_daytime_task(self, action, taskname, weekdays, monthdays, timeonday, processmethod, args, kw):
"""Add a new Day Task (Weekday or Monthday) to the schedule."""
if weekdays and monthdays:
@@ -223,7 +245,12 @@
print >>sys.stderr,"".join(traceback.format_exception(*sys.exc_info()))
print >>sys.stderr,"-"*20
+class OnetimeTask(Task):
+ """A task that only occurs once."""
+ def reschedule(self, scheduler):
+ pass
+
class IntervalTask(Task):
"""A repeated task that occurs at certain intervals (in seconds)."""
def __init__(self, name, interval, action, args=None, kw=None):
@@ -341,6 +368,9 @@
class ThreadedIntervalTask(ThreadedTaskMixin, IntervalTask):
"""Interval Task that executes in its own thread."""
pass
+ class ThreadedOnetimeTask(ThreadedTaskMixin, OnetimeTask):
+ """Onetime Task that executes in its own thread."""
+ pass
class ThreadedWeekdayTask(ThreadedTaskMixin, WeekdayTask):
"""Weekday Task that executes in its own thread."""
pass
@@ -399,6 +429,9 @@
class ForkedIntervalTask(ForkedTaskMixin, IntervalTask):
"""Interval Task that executes in its own process."""
pass
+ class ForkedOnetimeTask(ForkedTaskMixin, OnetimeTask):
+ """Onetime Task that executes in its own process."""
+ pass
class ForkedWeekdayTask(ForkedTaskMixin, WeekdayTask):
"""Weekday Task that executes in its own process."""
pass
@@ -433,6 +466,13 @@
kw=kw, initialdelay=initialdelay,
processmethod=processmethod, taskname=taskname)
+def add_onetime_task(action, args=None, kw=None,
+ initialdelay=0, processmethod=method.threaded, taskname=None):
+ si = _get_scheduler()
+ return si.add_onetime_task(action=action, args=args,
+ kw=kw, initialdelay=initialdelay,
+ processmethod=processmethod, taskname=taskname)
+
def add_weekday_task(action, weekdays, timeonday, args=None, kw=None,
processmethod=method.threaded, taskname=None):
si = _get_scheduler()