[Answer]-Celery periodic_task in Django run once(!)



You could use transactions:

def initialize_new_jobs():
    for obj in Queue.objects.select_for_update().filter(status__in=['I', 'Q']):
        obj.status = 'A'

select_for_update() puts an exclusive lock on rows so that other users get blocked when attempting to read the values. The lock is released after the transaction has been committed or rolled back. Reference.

This way you can be sure that the obj has status of I or Q and that the obj.save() will work properly.

👤Maciej Gol

Leave a comment