Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read/Write File Lock #380

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft

Read/Write File Lock #380

wants to merge 24 commits into from

Conversation

Yard1
Copy link

@Yard1 Yard1 commented Nov 23, 2024

Currently WIP. Need to add asyncio API and more tests.

This PR adds a ReadWriteFileLock API and implementation (currently only for Unix. I don't know if this is possible on Windows). ReadWriteFileLock allows the caller to operate in either READ or WRITE mode. The lock can be held by multiple READers at the same time, but a WRITEr is guaranteed to have exclusive access to the lock (across both READers and WRITErs). The lock is writer-preferred by employing an outer and inner lock (with the outer serving as staging for acquiring the inner - this means the outer lock is much easier to acquire).

In reader case:

  1. outer lock is acquired non-exclusively
  2. inner lock is acquired non-exclusively
  3. outer lock is released
  4. work is done
  5. inner lock is released

In writer case:

  1. outer lock is acquired exclusively
  2. inner lock is acquired exclusively
  3. work is done
  4. inner lock is released
  5. outer lock is released

While this seems to be working well in practice, there are no actual guarantees of ordering and priority.

I am not firmly settled on the API. Other idea would be to roll wrapper functionality into the base class and provide write_acquire and read_acquire functions (though the context managers would get a bit trickier - one would need to do with self.read_write_lock.read(): instead of simply self.read_write_lock: - as this doesn't allow specifying the mode)

Closes #307

Yard1 and others added 24 commits November 22, 2024 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Request add RwLock
1 participant