Package cherrypy :: Package lib :: Module locking
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.lib.locking

 1  import datetime 
2 3 4 -class NeverExpires(object):
5 - def expired(self):
6 return False
7
8 9 -class Timer(object):
10 """ 11 A simple timer that will indicate when an expiration time has passed. 12 """
13 - def __init__(self, expiration):
14 "Create a timer that expires at `expiration` (UTC datetime)" 15 self.expiration = expiration
16 17 @classmethod
18 - def after(cls, elapsed):
19 """ 20 Return a timer that will expire after `elapsed` passes. 21 """ 22 return cls(datetime.datetime.utcnow() + elapsed)
23
24 - def expired(self):
25 return datetime.datetime.utcnow() >= self.expiration
26
27 28 -class LockTimeout(Exception):
29 "An exception when a lock could not be acquired before a timeout period"
30
31 32 -class LockChecker(object):
33 """ 34 Keep track of the time and detect if a timeout has expired 35 """
36 - def __init__(self, session_id, timeout):
37 self.session_id = session_id 38 if timeout: 39 self.timer = Timer.after(timeout) 40 else: 41 self.timer = NeverExpires()
42
43 - def expired(self):
44 if self.timer.expired(): 45 raise LockTimeout( 46 "Timeout acquiring lock for %(session_id)s" % vars(self)) 47 return False
48