-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLock.py
More file actions
63 lines (41 loc) · 1.51 KB
/
Lock.py
File metadata and controls
63 lines (41 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
__author__ = 'bobrik'
from .Locker import LockerException
class LockReuseException(LockerException): pass
class LockWaitTimeoutException(LockerException): pass
class UnlockWithoutLockException(LockerException): pass
class LostLockException(LockerException): pass
class Lock(object):
def __init__(self, locker, name, sequence):
self.locker = locker
self.name = name
self.sequence = sequence
self.acquired = False
def acquire(self, wait, timeout):
if self.get_sequence() is None:
raise LockReuseException("Trying to reuse lock")
result = self.locker.lock(self.name, self.sequence, wait, timeout)
if not result:
raise LockWaitTimeoutException("Wait timeout exceed for lock")
self.acquired = True
def release(self, panic = False):
if not self.is_acquired():
raise UnlockWithoutLockException("Trying to ulock without lock")
result = self.locker.unlock(self.sequence)
self.reset()
if panic and not result:
raise LostLockException("Lost lock")
def reset(self):
if self.get_sequence():
self.locker.unregister(self)
self.sequence = None
self.acquired = False
def is_acquired(self):
return self.acquired
def get_sequence(self):
return self.sequence
def __del__(self):
if self.is_acquired():
self.release()
else:
if self.get_sequence():
self.reset()