PyXR

c:\python24\lib \ bsddb \ dbutils.py



0001 #------------------------------------------------------------------------
0002 #
0003 # Copyright (C) 2000 Autonomous Zone Industries
0004 #
0005 # License:      This is free software.  You may use this software for any
0006 #               purpose including modification/redistribution, so long as
0007 #               this header remains intact and that you do not claim any
0008 #               rights of ownership or authorship of this software.  This
0009 #               software has been tested, but no warranty is expressed or
0010 #               implied.
0011 #
0012 # Author: Gregory P. Smith <greg@electricrain.com>
0013 #
0014 # Note: I don't know how useful this is in reality since when a
0015 #       DBLockDeadlockError happens the current transaction is supposed to be
0016 #       aborted.  If it doesn't then when the operation is attempted again
0017 #       the deadlock is still happening...
0018 #       --Robin
0019 #
0020 #------------------------------------------------------------------------
0021 
0022 
0023 #
0024 # import the time.sleep function in a namespace safe way to allow
0025 # "from bsddb.db import *"
0026 #
0027 from time import sleep as _sleep
0028 
0029 import db
0030 
0031 # always sleep at least N seconds between retrys
0032 _deadlock_MinSleepTime = 1.0/64
0033 # never sleep more than N seconds between retrys
0034 _deadlock_MaxSleepTime = 3.14159
0035 
0036 # Assign a file object to this for a "sleeping" message to be written to it
0037 # each retry
0038 _deadlock_VerboseFile = None
0039 
0040 
0041 def DeadlockWrap(function, *_args, **_kwargs):
0042     """DeadlockWrap(function, *_args, **_kwargs) - automatically retries
0043     function in case of a database deadlock.
0044 
0045     This is a function intended to be used to wrap database calls such
0046     that they perform retrys with exponentially backing off sleeps in
0047     between when a DBLockDeadlockError exception is raised.
0048 
0049     A 'max_retries' parameter may optionally be passed to prevent it
0050     from retrying forever (in which case the exception will be reraised).
0051 
0052         d = DB(...)
0053         d.open(...)
0054         DeadlockWrap(d.put, "foo", data="bar")  # set key "foo" to "bar"
0055     """
0056     sleeptime = _deadlock_MinSleepTime
0057     max_retries = _kwargs.get('max_retries', -1)
0058     if _kwargs.has_key('max_retries'):
0059         del _kwargs['max_retries']
0060     while 1:
0061         try:
0062             return function(*_args, **_kwargs)
0063         except db.DBLockDeadlockError:
0064             if _deadlock_VerboseFile:
0065                 _deadlock_VerboseFile.write(
0066                     'dbutils.DeadlockWrap: sleeping %1.3f\n' % sleeptime)
0067             _sleep(sleeptime)
0068             # exponential backoff in the sleep time
0069             sleeptime *= 2
0070             if sleeptime > _deadlock_MaxSleepTime:
0071                 sleeptime = _deadlock_MaxSleepTime
0072             max_retries -= 1
0073             if max_retries == -1:
0074                 raise
0075 
0076 
0077 #------------------------------------------------------------------------
0078 

Generated by PyXR 0.9.4
SourceForge.net Logo