views:

3274

answers:

8

I've been working on a web app using Django, and I'm curious if there is a way to schedule a job to run periodically.

Basically I just want to run through the database and make some calculations/updates on an automatic, regular basis, but I can't seem to find any documentation on doing this.

Does anyone know how to set this up?

To clarify: I know I can set up a cron job to do this, but I'm curious if there is some feature in Django that provides this functionality. I'd like people to be able to deploy this app themselves without having to do much config (preferably zero).

I've considered triggering these actions "retroactively" by simply checking if a job should have been run since the last time a request was sent to the site, but I'm hoping for something a bit cleaner.

+8  A: 

If you're using a standard OS, you use cron.

If you're using Windows, you use at.

Write a Django management command to

  1. Figure out what platform they're on.

  2. Either execute the appropriate "AT" command for your users, or update the crontab for your users.

S.Lott
I'd like to have it rolled-up into my django app if possible.
TM
@TM: What does "rolled-up into my django app" mean? Please clarify your question.
S.Lott
I'd like people to be able to easily deploy this app without having to set up cron jobs themselves.
TM
You can always wrap the cron interface into your app.
monkut
Re reading this answer I'm wondering why I didn't upvote it a long time ago. +1
TM
+1 for standard OS
+22  A: 

One solution that I have employed is to do this:

1) Create a custom management command, e.g.

python manage.py my_cool_command

2) Use cron to run my command at the required times.

Brian Neal
+7  A: 

Look at Django Poor Man's Cron which is a Django app that makes use of spambots, search engine indexing robots and alike to run scheduled tasks in approximately regular intervals

See: http://code.google.com/p/django-poormanscron/

@jrogi: I hadn't seen this project before and that's an interesting concept to use bot requests as a scheduling mechanism. Thanks!
Van Gale
+2  A: 

I personally use cron, but the Jobs Scheduling parts of django-commands-extension looks interesting.

Van Gale
Still depends on cron for triggering, just adds another abstraction layer in between. Not sure it's worth it, personally.
Carl Meyer
I agree, and after thinking about it I don't want request middleware slowing down my site (ala poormanscron above) when cron can do the job better anyway.
Van Gale
+7  A: 

Interesting new pluggable Django app: django-chronograph

You only have to add one cron entry which acts as a timer, and you have a very nice Django admin interface into the scripts to run.

Van Gale
A: 

If you are a high-performance site and already using RabbitMQ here's a trick to get around cron:

Using AMQP to do cron-like scheduling

Van Gale
+7  A: 

Celery is a distributed task queue, built on AMQP (RabbitMQ). It also handles periodic tasks in a cron-like fashion. Depending on your app, it might be worth a gander.

dln
A: 

Put the following at the top of your cron.py file:

#!/usr/bin/python
import os, sys
sys.path.append('/path/to/') # the parent directory of the project
sys.path.append('/path/to/project') # these lines only needed if not on path
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings'

# imports and code below
Matt McCormick