A Python UPnP Media Server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
692 B

  1. import fcntl
  2. import shelve
  3. class FileLock(object):
  4. def __init__(self, fname = None):
  5. self.f = open(fname, 'w+')
  6. self.islocked = False
  7. def exclusivelock(self):
  8. fcntl.flock(self.f.fileno(), fcntl.LOCK_EX)
  9. self.islocked = True
  10. def sharedlock(self):
  11. fcntl.flock(self.f.fileno(), fcntl.LOCK_SH)
  12. self.islocked = True
  13. def unlock(self):
  14. fcntl.flock(self.f.fileno(), fcntl.LOCK_UN)
  15. self.islocked = False
  16. class LockShelve(FileLock, shelve.DbfilenameShelf):
  17. def __init__(self, fname, *args, **kwargs):
  18. FileLock.__init__(self, fname + ".lock")
  19. self.exclusivelock()
  20. try:
  21. shelve.DbfilenameShelf.__init__(self, fname, *args, **kwargs)
  22. except:
  23. self.f.close()
  24. raise