0

I am in the process of writing my own task app using Django and would like a few specific functions to be executed every day at a certain time (updating tasks, checking due dates, etc.). Is there a way to have Django run functions on a regular basis or how do I go about this in general?

Does it make sense to write an extra program with an infinite loop for this or are there better ways?

Basstma
  • 1
  • 1
  • I don't believe there is anything in DJango for that. You can look at cron jobs or something equivalent. – Noah May Nov 30 '22 at 21:06
  • Probably duplicate of [this](https://stackoverflow.com/questions/23563934/django-how-to-run-a-function-everyday) – ignacio Nov 30 '22 at 21:56
  • Does this answer your question? [Django - How to run a function EVERYDAY?](https://stackoverflow.com/questions/23563934/django-how-to-run-a-function-everyday) – ignacio Nov 30 '22 at 21:57
  • Does this answer your question? [Set up a scheduled job?](https://stackoverflow.com/questions/573618/set-up-a-scheduled-job) – rob Nov 30 '22 at 22:02

2 Answers2

0

Celery is a good option here:

  1. First steps with Django

  2. Periodic Tasks

    app.conf.beat_schedule = {
    'add-every-30-seconds': {
            'task': 'tasks.add',
            'schedule': 30.0,
            'args': (16, 16)
        },
    }
    app.conf.timezone = 'UTC'
    
    

With celery you can define periodic tasks at any given interval. Celery workers will then pick up those tasks when needed. You will need to run something like RabbitMQ or Redis to support the celery workers.

The alternative, simpler, way is to add an entry to your urls.py that catches any url you don't otherwise use, and use that as a prompt to check your database as to whether another task is due. This leverages the fact that your website will be hit by a lot of bot traffic. The timing isn't entirely reliable but it doesn't require any extra set up.

VMatić
  • 996
  • 2
  • 10
  • 18
0

you can use django-cronjobs or maybe Schedule your job with schedule library.

Fazi Alnjd
  • 21
  • 5