This is an old question, but since no one has sent an answer, here it is.
To repeat the queries if a deadlock occurred, I made the monkey run the “execute” method of the django CursorWrapper class. This method is called whenever a request is made, so it will work throughout ORM, and you don’t have to worry about deadlocks in your project:
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
What the code does above: it will try to start the request, and if the OperationalError statement is reset with error code 1213 (deadlock), it will wait 200 ms and try again. He will do this 3 times, and if after 3 times the problem is not resolved, the original exception will be raised.
This code should be executed when the django project is loaded into memory, so it’s convenient to place it in the __init__.py file of any of your applications (I put it in the __init__.py file of my main project directory - the one that has the same name as the project django).
Hope this helps anyone in the future.
Luccas correa
source share