【发布时间】:2021-08-18 04:18:33
【问题描述】:
在我的 Django 应用程序中,我有几十个并发的 Celery 工作人员从各种模型中创建、更新和删除数据。我遇到了以下错误:
deadlock detected
DETAIL: Process 3285070 waits for ShareLock on transaction 559341801; blocked by process 3285058.
Process 3285058 waits for ShareLock on transaction 559341803; blocked by process 3285070.
HINT: See server log for query details.
这些是引发错误的代码块:
with transaction.atomic():
for score in list_of_scores_to_update:
score.save()
time_delta = timezone.now() - timezone.timedelta(minutes=2)
with transaction.atomic():
Score.objects.select_for_update(of=('self'), skip_locked=True).filter(checked_date__lte=time_delta).delete()
如何通过在更新所有行之前获取所有行的写锁来防止这些死锁?
【问题讨论】:
标签: django postgresql django-models django-rest-framework deadlock