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.

34 lines
749 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 close(self):
  8. self.f.close()
  9. self.islocked = None
  10. def exclusivelock(self):
  11. fcntl.flock(self.f.fileno(), fcntl.LOCK_EX)
  12. self.islocked = True
  13. def sharedlock(self):
  14. fcntl.flock(self.f.fileno(), fcntl.LOCK_SH)
  15. self.islocked = True
  16. def unlock(self):
  17. fcntl.flock(self.f.fileno(), fcntl.LOCK_UN)
  18. self.islocked = False
  19. class LockShelve(FileLock, shelve.DbfilenameShelf):
  20. def __init__(self, fname, *args, **kwargs):
  21. FileLock.__init__(self, fname + ".lock")
  22. self.exclusivelock()
  23. try:
  24. shelve.DbfilenameShelf.__init__(self, fname, *args, **kwargs)
  25. except:
  26. self.close()
  27. raise