dbutils.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #------------------------------------------------------------------------
  2. #
  3. # Copyright (C) 2000 Autonomous Zone Industries
  4. #
  5. # License: This is free software. You may use this software for any
  6. # purpose including modification/redistribution, so long as
  7. # this header remains intact and that you do not claim any
  8. # rights of ownership or authorship of this software. This
  9. # software has been tested, but no warranty is expressed or
  10. # implied.
  11. #
  12. # Author: Gregory P. Smith <greg@krypto.org>
  13. #
  14. # Note: I don't know how useful this is in reality since when a
  15. # DBLockDeadlockError happens the current transaction is supposed to be
  16. # aborted. If it doesn't then when the operation is attempted again
  17. # the deadlock is still happening...
  18. # --Robin
  19. #
  20. #------------------------------------------------------------------------
  21. #
  22. # import the time.sleep function in a namespace safe way to allow
  23. # "from bsddb.dbutils import *"
  24. #
  25. from time import sleep as _sleep
  26. import sys
  27. absolute_import = (sys.version_info[0] >= 3)
  28. if absolute_import :
  29. # Because this syntaxis is not valid before Python 2.5
  30. exec("from . import db")
  31. else :
  32. import db
  33. # always sleep at least N seconds between retrys
  34. _deadlock_MinSleepTime = 1.0/128
  35. # never sleep more than N seconds between retrys
  36. _deadlock_MaxSleepTime = 3.14159
  37. # Assign a file object to this for a "sleeping" message to be written to it
  38. # each retry
  39. _deadlock_VerboseFile = None
  40. def DeadlockWrap(function, *_args, **_kwargs):
  41. """DeadlockWrap(function, *_args, **_kwargs) - automatically retries
  42. function in case of a database deadlock.
  43. This is a function intended to be used to wrap database calls such
  44. that they perform retrys with exponentially backing off sleeps in
  45. between when a DBLockDeadlockError exception is raised.
  46. A 'max_retries' parameter may optionally be passed to prevent it
  47. from retrying forever (in which case the exception will be reraised).
  48. d = DB(...)
  49. d.open(...)
  50. DeadlockWrap(d.put, "foo", data="bar") # set key "foo" to "bar"
  51. """
  52. sleeptime = _deadlock_MinSleepTime
  53. max_retries = _kwargs.get('max_retries', -1)
  54. if 'max_retries' in _kwargs:
  55. del _kwargs['max_retries']
  56. while True:
  57. try:
  58. return function(*_args, **_kwargs)
  59. except db.DBLockDeadlockError:
  60. if _deadlock_VerboseFile:
  61. _deadlock_VerboseFile.write(
  62. 'dbutils.DeadlockWrap: sleeping %1.3f\n' % sleeptime)
  63. _sleep(sleeptime)
  64. # exponential backoff in the sleep time
  65. sleeptime *= 2
  66. if sleeptime > _deadlock_MaxSleepTime:
  67. sleeptime = _deadlock_MaxSleepTime
  68. max_retries -= 1
  69. if max_retries == -1:
  70. raise
  71. #------------------------------------------------------------------------