开发者

Django: set up a scheduled job within Django?

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/di开发者_高级运维sadvantages of doing this versus using cron + a Django management command?

thanks!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜