Django related objects are missing from celery task (race condition?)

后端 未结 3 1271
囚心锁ツ
囚心锁ツ 2021-01-24 22:03

Strange behavior, that I don\'t know how to explain. I\'ve got a model, Track, with some related points. I call a celery task to performs some calculat

3条回答
  •  一生所求
    2021-01-24 22:37

    I'm going to assume this is due to transaction isolation.

    Django transactions by default are tied to requests; and while a transaction is active, no other process will see the changes until the transaction is committed. If you're in the middle of a save method, and there are quite a lot of other actions that take place before the request finishes, it seems likely that Celery starts processing the task before the transaction is committed. You could fix this by committing manually or by delaying the task.

提交回复
热议问题