spyce
         
home     documentation     download     Spyce logo


Documentation - Runtime

Prev: 3.7 - Server utilities Up: 3.7 - Server utilities Next: 3.7.2 - spyceUtil

3.7.1. The Spyce scheduler

Spyce provides a scheduler that allows you to easily define tasks to run at specified times or intervals inside the Spyce server. This allows your tasks to leverage the tools Spyce gives you, as well as any global data your application maintains within Spyce, such as cached data or database connection pools. This also has the advantage (over, say, crontab entries) of keeping your application self-contained, making it easier to deploy changes from a development machine to production.

The Spyce scheduler is currently only useful if you are running in webserver mode. If you run under mod_python, CGI, or FastCGI, you could approximate scheduler behavior by storing tasks and checking to see if any are overdue with every request received; this would be an excellent project for someone wishing to get started in Spyce development.

 
 
scheduler
index
/var/spyce-2.1/scheduler.py

A module for scheduling arbitrary callables to run at given times
or intervals, modeled on the naviserver API.  Scheduler runs in
its own thread; callables run in this same thread, so if you have
an unusually long callable to run you may wish to give it its own
thread, for instance,
 
schedule(3600, lambda: threading.Thread(target=longcallable).start())
 
Scheduler does not provide subsecond resolution.
 
Public functions are threadsafe.

 
Modules
       
atexit
sys
threading
time
traceback

 
Classes
       
Task

 
class Task
    Instantiated by the schedule methods.
 
Instance variables:
  nextrun: epoch seconds at which to run next
  interval: seconds before repeating
  callable: function to invoke
  last: if True, will be unscheduled after nextrun
 
(Note that by manually setting last on a Task instance, you
can cause it to run an arbitrary number of times.)
 
  Methods defined here:
__init__(self, firstrun, interval, callable, once)
__repr__(self)
examples/scheduling.py
import spyce, scheduler

def delete_unsubmitted():
    db = spyce.SPYCE_GLOBALS['dbpool'].connection()
    sql = "DELETE FROM alerts WHERE status = 'unsubmitted' AND created < now() - '1 week'::interval"
    db.execute(sql)

# delete alerts that were created over a week ago but but still not submitted 
scheduler.schedule_daily(00, 10, delete_unsubmitted)


Prev: 3.7 - Server utilities Up: 3.7 - Server utilities Next: 3.7.2 - spyceUtil


Spyce logo
Python Server Pages
version 2.1.3
Spyce Powered SourceForge Logo