|
|
@@ -0,0 +1,29 @@ |
|
|
|
import fcntl |
|
|
|
import shelve |
|
|
|
|
|
|
|
class FileLock(object): |
|
|
|
def __init__(self, fname = None): |
|
|
|
self.f = open(fname, 'w+') |
|
|
|
self.islocked = False |
|
|
|
|
|
|
|
def exclusivelock(self): |
|
|
|
fcntl.flock(self.f.fileno(), fcntl.LOCK_EX) |
|
|
|
self.islocked = True |
|
|
|
|
|
|
|
def sharedlock(self): |
|
|
|
fcntl.flock(self.f.fileno(), fcntl.LOCK_SH) |
|
|
|
self.islocked = True |
|
|
|
|
|
|
|
def unlock(self): |
|
|
|
fcntl.flock(self.f.fileno(), fcntl.LOCK_UN) |
|
|
|
self.islocked = False |
|
|
|
|
|
|
|
class LockShelve(FileLock, shelve.DbfilenameShelf): |
|
|
|
def __init__(self, fname, *args, **kwargs): |
|
|
|
FileLock.__init__(self, fname + ".lock") |
|
|
|
self.exclusivelock() |
|
|
|
try: |
|
|
|
shelve.DbfilenameShelf.__init__(self, fname, *args, **kwargs) |
|
|
|
except: |
|
|
|
self.f.close() |
|
|
|
raise |