How to lock a file but be able to read it
CodingOctocat opened this issue · 4 comments
I need a EXCLUSIVE and BLOCKING lock, and i also need read file, and i get PermissionError: [Errno 13] Permission denied
.
I am a multi-process program (multi .py), so each process can only communicate with each other via files.
Win11
I created a context manager:
FileLocker.py
def __enter__(self):
self._file = open(self.filename, self._mode, **self._file_open_kwargs)
portalocker.lock(self._file, flags=self._flags)
return self._file
def __exit__(self, exc_type, exc_val, exc_tb):
portalocker.unlock(self._file)
Usage:
with FileLocker('.lock', 'a+') as fp:
fp.write('test')
Try read file:
with open('.lock', 'r') as fp:
lines = fp.readlines()
# raise PermissionError: [Errno 13] Permission denied
To solve this problem, I changed the strategy so that the '.lock' file is only used to control locking, and another file ('.writelock') is used to read and write, so that only one process can write to the '.writelock' file at the same time, but the contents can be read at the same time.
Sorry about the slow response, I didn't see your reply earlier.
This is mostly a windows issue I believe, but if you use non-exclusive locking you should have the behaviour you're looking for.
When you're writing, lock the file, when you're reading, don't lock the file :)