MPTT Tree ID
- Django + mysql: transaction.atomic() で select_for_update() を使ってレコードをロックしたときのメモ
- MySQL で採番機能(シーケンス)を実装する方法を整理する
- MySQL のロックについて公式ドキュメントを読みながら動作検証してみた〜テーブルレベルロック〜
分散ロック
from django.core.cache import cache
with cache.lock("somekey"):
do_some_thing()
redis-py:
- https://github.com/redis/redis-py/blob/d3a3ada03e080f39144807c9fbe44876c40e0548/redis/client.py#L394
デッドロック
import django.db.backends.utils
from django.db import OperationalError
import time
original = django.db.backends.utils.CursorWrapper.execute
def execute_wrapper(*args, **kwargs):
attempts = 0
while attempts < 3:
try:
return original(*args, **kwargs)
except OperationalError as e:
code = e.args[0]
if attempts == 2 or code != 1213:
raise e
attempts += 1
time.sleep(0.2)
django.db.backends.utils.CursorWrapper.execute = execute_wrapper