I've serched among the internet a working example of a scheduled job in Django. But I can only find how to do it, but no example is given. Can someone share a working example of the Django framework running a shecudled task with cron?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Django __str__ returned non-string (type NoneType)
- Evil ctypes hack in python
Scheduled task can be done through
celery
.Celery is a task queue with focus on real-time processing, while also supporting task scheduling.
You should try to add the following code block at the beginning of your python script which uses anything of the django app.
Then you should be able to call this script in a cronjob like
First of all create a custom admin command. This command will be used to add the task to the crontab. Here is an example of my custom command:
cron.py
After that, if you have a look at the code below, a python script is going to be invoked every 1 minute. Create an
example.py
file and add it there the functionality that you want to be made every 1 minute.All is prepared to add the scheduled job, just invoke the following command from the project django directory:
To verify that the cron job was added successfully invoke the following command:
You should see something like this:
To debug the example.py, simply invoke this coomand: