Set up a scheduled job?

后端 未结 24 2534
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 01:13

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 dat

24条回答
  •  死守一世寂寞
    2020-11-22 02:07

    I just thought about this rather simple solution:

    1. Define a view function do_work(req, param) like you would with any other view, with URL mapping, return a HttpResponse and so on.
    2. Set up a cron job with your timing preferences (or using AT or Scheduled Tasks in Windows) which runs curl http://localhost/your/mapped/url?param=value.

    You can add parameters but just adding parameters to the URL.

    Tell me what you guys think.

    [Update] I'm now using runjob command from django-extensions instead of curl.

    My cron looks something like this:

    @hourly python /path/to/project/manage.py runjobs hourly
    

    ... and so on for daily, monthly, etc'. You can also set it up to run a specific job.

    I find it more managable and a cleaner. Doesn't require mapping a URL to a view. Just define your job class and crontab and you're set.

提交回复
热议问题