Strange behaviour of Celery when using Django ORM and concurrency > 1 #9778
-
Versions:
I have Django application with two models: class B(models.Model):
attributes = models.JSONField()
class A(models.Model):
b = models.ForeignKey(B) I have a celery task that will be executed in default queue: ...
celery_app = Celery("foo")
@celery_app.task
def my_task():
b, _ = B.objects.get_or_create(id=1, defaults={"attributes": {}})
_, created = A.objects.update_or_create(id=1, defaults={"b": b} When celery worker is running with
And there are no B object created in DB after getting this error. Django settings for DB are simple: DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "foo",
"USER": "user",
"PASSWORD": "password",
"HOST": "localhost",
"PORT": "5432",
}
} It happens only if executing celery worker with |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
you need to check if your schema migrations generated are rendered in the database or not or the models are properly designed. IntegrityError is generally related to db issues. https://www.geeksforgeeks.org/python/integrityerror-in-django/ |
Beta Was this translation helpful? Give feedback.
-
@auvipy Models and migrations are okay and not the reason of this behavior. As I mentioned previously this code works fine when celery concurrency set to 1. |
Beta Was this translation helpful? Give feedback.
-
Issue is gone when celery worker launched with |
Beta Was this translation helpful? Give feedback.
Issue is gone when celery worker launched with
-P gevent
(https://docs.celeryq.dev/en/latest/userguide/concurrency/gevent.html)