Reuse lock file
This commit is contained in:
parent
49bc0728e3
commit
1ea9a33101
@ -65,6 +65,13 @@ if os.name == "nt":
|
|||||||
ctypes.wintypes.DWORD,
|
ctypes.wintypes.DWORD,
|
||||||
ctypes.POINTER(Overlapped)]
|
ctypes.POINTER(Overlapped)]
|
||||||
lock_file_ex.restype = ctypes.wintypes.BOOL
|
lock_file_ex.restype = ctypes.wintypes.BOOL
|
||||||
|
unlock_file_ex = ctypes.windll.kernel32.UnlockFileEx
|
||||||
|
unlock_file_ex.argtypes = [ctypes.wintypes.HANDLE,
|
||||||
|
ctypes.wintypes.DWORD,
|
||||||
|
ctypes.wintypes.DWORD,
|
||||||
|
ctypes.wintypes.DWORD,
|
||||||
|
ctypes.POINTER(Overlapped)]
|
||||||
|
unlock_file_ex.restype = ctypes.wintypes.BOOL
|
||||||
elif os.name == "posix":
|
elif os.name == "posix":
|
||||||
import fcntl
|
import fcntl
|
||||||
|
|
||||||
@ -519,6 +526,7 @@ class Collection(BaseCollection):
|
|||||||
|
|
||||||
_lock = threading.Condition()
|
_lock = threading.Condition()
|
||||||
_lock_file = None
|
_lock_file = None
|
||||||
|
_lock_file_locked = False
|
||||||
_readers = 0
|
_readers = 0
|
||||||
_writer = False
|
_writer = False
|
||||||
|
|
||||||
@ -556,6 +564,7 @@ class Collection(BaseCollection):
|
|||||||
os.chmod(lock_path, stat.S_IWUSR | stat.S_IRUSR)
|
os.chmod(lock_path, stat.S_IWUSR | stat.S_IRUSR)
|
||||||
except OSError:
|
except OSError:
|
||||||
cls.logger.debug("Failed to set permissions on lock file")
|
cls.logger.debug("Failed to set permissions on lock file")
|
||||||
|
if not cls._lock_file_locked:
|
||||||
if os.name == "nt":
|
if os.name == "nt":
|
||||||
handle = msvcrt.get_osfhandle(cls._lock_file.fileno())
|
handle = msvcrt.get_osfhandle(cls._lock_file.fileno())
|
||||||
flags = LOCKFILE_EXCLUSIVE_LOCK if mode == "w" else 0
|
flags = LOCKFILE_EXCLUSIVE_LOCK if mode == "w" else 0
|
||||||
@ -568,6 +577,7 @@ class Collection(BaseCollection):
|
|||||||
fcntl.lockf(cls._lock_file.fileno(), _cmd)
|
fcntl.lockf(cls._lock_file.fileno(), _cmd)
|
||||||
except OSError:
|
except OSError:
|
||||||
cls.logger.debug("Locking not supported")
|
cls.logger.debug("Locking not supported")
|
||||||
|
cls._lock_file_locked = True
|
||||||
yield
|
yield
|
||||||
with cls._lock:
|
with cls._lock:
|
||||||
if mode == "r":
|
if mode == "r":
|
||||||
@ -575,6 +585,15 @@ class Collection(BaseCollection):
|
|||||||
else:
|
else:
|
||||||
cls._writer = False
|
cls._writer = False
|
||||||
if cls._readers == 0:
|
if cls._readers == 0:
|
||||||
cls._lock_file.close()
|
if os.name == "nt":
|
||||||
cls._lock_file = None
|
handle = msvcrt.get_osfhandle(cls._lock_file.fileno())
|
||||||
|
overlapped = Overlapped()
|
||||||
|
if not unlock_file_ex(handle, 0, 1, 0, overlapped):
|
||||||
|
cls.logger.debug("Unlocking not supported")
|
||||||
|
elif os.name == "posix":
|
||||||
|
try:
|
||||||
|
fcntl.lockf(cls._lock_file.fileno(), fcntl.LOCK_UN)
|
||||||
|
except OSError:
|
||||||
|
cls.logger.debug("Unlocking not supported")
|
||||||
|
cls._lock_file_locked = False
|
||||||
cls._lock.notify()
|
cls._lock.notify()
|
||||||
|
Loading…
Reference in New Issue
Block a user