TurboGears includes a scheduler that is based on Kronos by Irmen de Jong. This scheduler makes it easy to have one time or recurring tasks run as needed.
You can schedule Python functions to be called at specific intervals or days. It uses the standard sched module for the actual task scheduling, but provides much more:
To use the scheduler, set the
tg.scheduler config variable to
[global] configuration. This tells TurboGears to start the scheduler
when the server starts.
# Set to True if the scheduler should be started tg.scheduler = True
There are four functions in the
turbogears.schedule module you can use to
schedule jobs. They are called
All four functions return a
Task object. If you hold on to that
object, you can later cancel it by calling
Here is an example that runs the function
"do_something" every ten seconds:
from turbogears import scheduler def do_something(): print "Hello world." scheduler.add_interval_task(action=do_something, taskname='do_something', initialdelay=0, interval=10)
All four scheduling functions take the following arguments:
task.name), which can help if you’re trying to keep track of many tasks.
By default, each task will be run in a new thread. You can also pass in
turbogears.scheduler.method.forked. The default is
Sequential means that the task will run in the same thread as the scheduler, and task will be execuetd sequentially, one after another. This should only be used for quick tasks.
Forked means to fork a new process to run the job, which is sometimes more effective for intense jobs, particularly on multiprocessor machines (due to Python’s architecture).
it is impossible to add new tasks to a
after the scheduler has been started!
Here’s an example of how to schedule the same function as above as a task using the sequential method:
turbogears.scheduler.add_interval_task( processmethod=turbogears.scheduler.method.sequential, action=do_something, taskname='do_something', initialdelay=5, interval=60)
In addition to these common parameters, the four scheduling functions each offer additional options to determine when they run. Here are the four functions and their parameters for how often to run:
initialdelay with a number of seconds to wait before running and
interval with the number of seconds between runs.
For example, an initialdelay of 600 and interval of 60 would mean “start running after 10 minutes and run every 1 minute after that”.
weekdaysfrom 1-7 (where 1 is Monday). Additionally, you need to pass in
timeondaywhich is the time of day to run.
timeondayshould be a tuple with (hour, minute).
monthdaysfrom 1-31, and also pass in
timeondaywhich is an (hour, minute) tuple of the time of day to run the task.
initialdelaywith a number of seconds to wait before running.
For more control you can create one of the following
Task sub-classes support the following methods:
You can the schedule a task using one of the following methods of a
Scheduler instance. To get and instance, you can either call
turbogears.schedule._get_scheduler() or create your own instance of one of
the following Schedule classes:
schedule_task(self, task, delay)
schedule_task_abs(self, task, abstime)
You might want to use the scheduler as a kind of mini-cron to execute tasks at regular intervals or to dynamically schedule new tasks during runtime (e.g, a RSS reader). Where you place the code for your tasks and scheduling them will depend on your application. In any case you will need to run a function to (re) schedule your tasks after application startup.
For example, you could use the scheduler to regularly run jobs ranging from
reporting to updating the database using external data. For this purpose, you
might create a
jobs.py file in your applications package, containing a
function per ‘job’ and
schedule() function to schedule the tasks during
jobs.py basically reads as follows:
# jobs.py import datetime from turbogears.scheduler import add_weekday_task, add_interval_task def generate_product_ranking(): # do something useful here... pass def synchronize_stock(from=None): # do something useful here... pass def schedule(): add_weekday_task(action=generate_product_ranking, weekdays=range(1,8), timeonday=(0)) add_interval_task(action=synchronize_stock, args=[lambda:datetime.datetime.now() - datetime.timedelta(minutes=2)], interval=120)
Then add two lines to the
start() function in
commands.py, just before
the TurboGears server is started, to run the schedule function at each startup
(assuming your applications package is called
# commands.py ... def start(): ... # following two lines added from yourpkg import jobs turbogears.startup.call_on_startup.append(jobs.schedule) from yourpkg.controllers import Root turbogears.start_server(Root())