Home > Blockchain >  Execute task at specific times django
Execute task at specific times django

Time:12-01

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?

CodePudding user response:

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.

CodePudding user response:

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

  • Related