django cron choice

django cron choice,第1张

jgorset/django-kronos

http://stackoverflow.com/questions/3200001/using-crontab-with-djangoKronos

Kronos makes it really easy to schedule tasks with cron.

UsageDefine tasks

Kronos collects tasks from cron modules in your project root and each of your applications:

# app/cron.py

import kronos

@kronos.register('0 0 * * *')
def complain():
    complaints = [
        "I forgot to migrate our applications's cron jobs to our new server! Darn!",
        "I'm out of complaints! Damnit!"
    ]

    print random.choice(complaints)
Run tasks manually
$ python manage.py runtask complain
I forgot to migrate our applications's cron jobs to our new server! Darn!
Register tasks with cron
$ python manage.py installtasks
Installed 1 task.
Installation
$ pip install django-kronos

... and add kronos to INSTALLED_APPS.

Contribute
  • Fork the repository.
  • Do your thing.
  • Open a pull request.
  • Receive cake.
I love you

Johannes Gorset made this. You should tweet me if you can't get it
to work. In fact, you should tweet me anyway.

Creating a Django cron job « Slowchop Studios

Creating a Django cron job

Sunday, September 17, 2006

I couldn’t find a way to directly call a Python function in a Django application view from the command line. It doesn’t seem like it is a common thing to do from my Google search attempts. In this example I have a function defined to download a few web sites once a day using a custom made Django model. The project is called mytestproject and the application is called mytestapp. Here is the views.py file:

import urllib2
from models import WebSite
def daily_job():
    for site in WebSite.objects.all():
        page_html = urllib2.urlopen(site.url).read()
        do_something_with_page_html(page_html)

To run this function from the command line an optimist would create a python script that looks like this:

#!/usr/bin/env python
from mytestapp.views import daily_job
daily_job()

Running this will give you an exception about your DJANGO_SETTINGS_MODULE environment variable not being defined:

EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.

Lets change the script a little to conform with Django’s demands.

#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from mytestapp.views import daily_job
daily_job()

Please note that according to the documentation, DJANGO_SETTINGS_MODULE should be ‘mytestproject.settings’ instead of just ‘settings’. Using the project name in DJANGO_SETTINGS_MODULE will cause troubles in our situation because Django does some tricky things to your path before importing the settings module. For our needs it isn’t necessary to do this.

Of course you can make this script a little more generic so you can run an arbitrary script from your cron job if you feel the need to:

import sys
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

module_name = sys.argv[1]
function_name = ' '.join(sys.argv[2:])

exec('import %s' % module_name)
exec('%s.%s' % (module_name, function_name))

I’ll call this script run.py. To run my daily_job function:

python run.py mytestapp.views daily_job()

And your modnight cron job entry should look something like:

0 0 * * * python /path/to/mytestproject/run.py mytestapp.views daily_job()

And there you have it. I hope this will save you some time!

About the Author

Gerald Kaszuba

You can follow me on Google+, Twitter (@gakman) or contact me directly on email. More information about me, my projects and my résumé can be found on geraldkaszuba.com.

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/2089812.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-07-22
下一篇 2022-07-22

发表评论

登录后才能评论

评论列表(0条)

保存