1

Like this question (Django - set up a scheduled job) I want to run a regular task within Django.

I'd also really like to bundle it within Django if possible, rather than requiring a cron job. Ideally I'd like to handle the client a Django app that they can plug and play and move across servers, without needing to edit the crontab each time.

So, I'd like some advice. Could I bundle something like the following with Django, and hook into Django's startup process somehow? (pseudocode)

Function secondsUntilNextRun() {
     $a = getTimeValue(“Next Friday at 9am”)
     $b = getCurrentTimeValue()
     Return $a - $b
} 
OnStartup {
     $timeToSleep = secondsUntilNextRun()
     Start Background Thread
}  
Background Thread {
     Sleep($timeToSleep)
     DoEmailReminders()
     $timeToSleep = secondsUntilNextRun()
}

And what would be the advantages/disadvantages of doing this versus using cron + a Django management command?

thanks!

Community
  • 1
  • 1
AP257
  • 89,519
  • 86
  • 202
  • 261
  • possible duplicate of [Django - Set Up A Scheduled Job?](http://stackoverflow.com/questions/573618/django-set-up-a-scheduled-job) – Josh Smeaton Mar 08 '11 at 10:53
  • This is the exact same question, and you even linked to it. Read the second answer on the other question - it refers to Celery. – Josh Smeaton Mar 08 '11 at 10:53
  • Thanks. I'd read that answer but hadn't quite understood what Celery was or that it did what I needed. Still not sure I really understand, but I'll give it a go :) – AP257 Mar 08 '11 at 11:59
  • The client is really pushing me to bundle a script with Django rather than use anything external, e.g. Celery. How would I do this, could I do this, and what would be the disadvantages? – AP257 Mar 08 '11 at 12:21

1 Answers1

2

Have a look at the Celery Integration for Django. I use it for my projects and its working very well.

If you integrate your task directly into your Django code, they will run in the same context as your web application. This is probably not what your are looking for.

Reto Aebersold
  • 16,306
  • 5
  • 55
  • 74