16👍
These answers need to be updated. Django now has transaction.on_commit()
which is built for this exact problem, they even provide an example with a task:
transaction.on_commit(lambda: some_celery_task.delay('arg1'))
https://docs.djangoproject.com/en/2.1/topics/db/transactions/#django.db.transaction.on_commit
7👍
I’ve had the same problem in my code. After long investigation, I found out that race condition was happening because I was using @transaction.commit_on_success decorator. So the transaction was committed only after view returned. Which was happening after I was calling celery task.
Once I’ve removed “commit_on_success” decorator, everything started to work as expected. Because Django’s default transaction behavior is to commit transaction after any database altering operation.
You might also want to make sure you are not using TransactionMiddleware, because it does similar thing to @transaction.commit_on_success decorator. If you want to keep using it, you should look into using @transaction.autocommit decorator in your views with celery tasks, or @transaction.commit_manually.
- [Django]-Allowing RabbitMQ-Server Connections
- [Django]-Determine variable type within django template
- [Django]-Django multiple template inheritance – is this the right style?