lock.py (778B)
1 # standard imports 2 import time 3 4 # local imports 5 from .error import BackendError 6 7 BASE_DELAY = 0.01 8 BASE_DELAY_LIMIT = 10.0 9 10 11 class StoreLock: 12 13 def __init__(self, delay=BASE_DELAY, delay_limit=BASE_DELAY_LIMIT, error=BackendError, description=None): 14 self.base_delay = delay 15 self.delay = delay 16 self.delay_limit = delay_limit 17 self.error = error 18 self.description = description 19 20 21 def again(self, e=None): 22 if self.delay > self.delay_limit: 23 err = None 24 if e != None: 25 err = str(e) 26 else: 27 err = self.description 28 raise self.error(err) 29 time.sleep(self.delay) 30 self.delay *= 2 31 32 33 def reset(self): 34 self.delay = self.base_delay