How to dynamically schedule tasks in Django?
I need to create an application in Django that allows the user to perform some task every day at the time they specify at runtime.
I looked at Celery but couldn't find anything that helps. I found apply_async and I can get the task to execute once with a specific duration, but not repeat. I'm missing something, but I don't know what.
Please suggest how I can achieve this.
source to share
A simple solution that will prevent a large AMQP stack from being used by preventing external type dependencies Celery
. One thing you can do is that you can write a custom management command ,
python manage.py my_daily_cmd
If you are using windows use cron , if you are using windows use at or schtasks.exe ,
cron / at / schtasks.exe, you can run my_daily_cmd
at your desired time.
source to share
The advanced Python scheduler might be the solution for you.
https://github.com/agronholm/apscheduler
https://apscheduler.readthedocs.io/en/latest/
APScheduler can add jobs dynamically.
You can see an example on github: https://github.com/easytrader/Apscheduler_for_django/blob/master/trips/views.py
source to share
There is a package django-celery-beat
that allows you to dynamically add tasks to the database and then they are executed as you defined in the database (for example, every 5 minutes). But they currently have a bug whereby this task is not added to the celery queue when added to the database. One suggested solution is to restart the celery process every time a new task is added.
I solved it with Dan Baders package schedule
. I was scheduling a task every minute that checks the database for tasks that need to be done in the current minute. Then I start each of these tasks on a new thread.
Hope this helps.
source to share