[Fixed]-Non-periodic repetition of task in Django using celery

1👍

You really shouldn’t put an endless loop in a Celery task. If you need a dedicated process it would be better to run it on it’s own rather than through Celery.

You could reschedule a new task in the future whenever your task runs. Something like the following:

@app.task
def create_notifications():
     try:
          #
     finally:
          t = random.randint(45, 85)
          create_notifications.apply_async(countdown=t)

Another option would be to have a scheduler task that runs on a regular schedule but queues the notification tasks for a random time in the near future. For example, if the scheduler task runs every 45 seconds.

@app.task
def schedule_notifications():
     t = random.randint(0, 40)
     create_notifications.apply_async(countdown=t)
👤joshua

Leave a comment