Dead lock found in django transaction with select_for_update function
I have two functions: one for updating data and one for reading data. When they run concurrently in two threads, a deadlock occurs. Does anyone know why? The database's isolation level is set to REPEATABLE-READ. sleep function is used to increase the likelihood of a deadlock occurring.
@transaction.atomic()
def func_change_data():
print("start")
time.sleep(10)
boos = Boo.objects.select_for_update(skip_locked=True).filter(pk=xxx)
if not changes.exists():
return
print("sleep")
time.sleep(10)
boo = boos.first()
boo.stage = 'stage_2'
boo.save()
print("end")
@transaction.atomic()
def func__read():
boo = Boo.objects.select_for_update().get(
pk=xxx,
target_branch='master',
stage='stage_1'
)
time.sleep(10)
print("end sleep")