tox-dev / filelock

A platform-independent file lock for Python.
https://py-filelock.readthedocs.io
The Unlicense
752 stars 107 forks source link

Allow multiple readers #58

Open cppntn opened 4 years ago

cppntn commented 4 years ago

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.

chrysn commented 4 years ago

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

hmeine commented 4 years ago

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 commented 4 years ago

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

gaborbernat commented 2 years ago

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