Skip to content

Instantly share code, notes, and snippets.

@Zuckonit
Created September 4, 2014 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zuckonit/e52b2c36c337aaa8948a to your computer and use it in GitHub Desktop.
Save Zuckonit/e52b2c36c337aaa8948a to your computer and use it in GitHub Desktop.
deadlock.py
#
from time import sleep as _sleep
import db
# always sleep at least N seconds between retrys
_deadlock_MinSleepTime = 1.0/64
# never sleep more than N seconds between retrys
_deadlock_MaxSleepTime = 3.14159
# Assign a file object to this for a "sleeping" message to be written to it
# each retry
_deadlock_VerboseFile = None
def DeadlockWrap(function, *_args, **_kwargs):
"""DeadlockWrap(function, *_args, **_kwargs) - automatically retries
function in case of a database deadlock.
This is a function intended to be used to wrap database calls such
that they perform retrys with exponentially backing off sleeps in
between when a DBLockDeadlockError exception is raised.
A 'max_retries' parameter may optionally be passed to prevent it
from retrying forever (in which case the exception will be reraised).
d = DB(...)
d.open(...)
DeadlockWrap(d.put, "foo", data="bar") # set key "foo" to "bar"
"""
sleeptime = _deadlock_MinSleepTime
max_retries = _kwargs.get('max_retries', -1)
if _kwargs.has_key('max_retries'):
del _kwargs['max_retries']
while 1:
try:
return function(*_args, **_kwargs)
except db.DBLockDeadlockError:
if _deadlock_VerboseFile:
_deadlock_VerboseFile.write(
'dbutils.DeadlockWrap: sleeping %1.3f\n' % sleeptime)
_sleep(sleeptime)
# exponential backoff in the sleep time
sleeptime *= 2
if sleeptime > _deadlock_MaxSleepTime:
sleeptime = _deadlock_MaxSleepTime
max_retries -= 1
if max_retries == -1:
raise
#https://raw.githubusercontent.com/wpjunior/proled/1c81471295a831b0970085c44e66172a63c3a2b0/Python-2.4/lib/python2.4/bsddb/dbutils.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment