I have three Celery @tasks in my tasks.py file that are often queued and processed simultaneously by separate workers with similar processing times for each. The problem that I believe I am running into is that they are all trying to update the same user profile object before the other ones are complete. It appears that the last of the three processes to finish is the one that successfully writes to the database. If I run these with a few seconds in between the tasks all finish fine.
Any idea what is the problem or what is a way to keep trying to save to the profile until it actually works?
Thanks in advance for your help!
Django ORM can play a trick here. If you use model_object.save()
method, it updates all the fields. If your tasks are updating different fields in the same object, you may consider using ModelClass.objects.filter(pk=model_id).update(some_field=some_value)
, but here you may fall into how different RDBMS implement table/row locking.
Another option is to use Celery Chord and update user profile on completion of all tasks fetching user data. You may need to implement a distributed semaphore, so the only chord task will be executing for the same user profile at the same time.