ยปCore Development>Code coverage>Lib/bsddb/dbutils.py

Python code coverage for Lib/bsddb/dbutils.py

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