filelock icon indicating copy to clipboard operation
filelock copied to clipboard

Allow multiple readers

Open cppntn opened this issue 5 years ago • 4 comments

Is there a way to add an argument to the LockFile class, such as "writer" or "reader", in order to:

  1. allow multiple readings between processes and threads (like with LockFile("file.txt", mode="reader"):) avoiding writing

  2. not allow readings while writing (like with LockFile("file.txt", mode="writer"):)

Thanks

Edit: the operations are not referred to the file "file.txt", that file is just used as a locker.

cppntn avatar Jan 24 '20 11:01 cppntn

For the benefit of searchability, this is also called a readers-writers-lock, or an RW-lock.

chrysn avatar Jan 24 '20 12:01 chrysn

I would be interested in the same thing.

Implementation-wise, it would be possible to do this on top of the current classes: One could have a regular filelock around a shared file that contains the current number of readers / a writer flag. However, that would require three files, and some code would need to compose three corresponding filenames. Hence, if it was possible to have such a simple API as suggested above, that would be welcome!

hmeine avatar Feb 13 '20 08:02 hmeine

Are there alternative terms to be considered? I like mode = "reader" / "writer", but could also imagine "shared" / "exclusive", for instance.

hmeine avatar Feb 13 '20 08:02 hmeine

Hello, if you make a PR for this (with tests) we would be happy to review, thanks!

gaborbernat avatar Sep 27 '21 08:09 gaborbernat