python-trio / python-trio/trio

Provide Read-Write lock ?

Open
#528 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.3k
Forks
431
Avg merge
2d 17h
Merged PRs (30d)
6

Description

Related to #19

I've implemented a reader-writer lock (following https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock#Implementation) for my own needs.

@attr.s(slots=True)
class ReadLockManager:

    rwlock = attr.ib()

    async def __aenter__(self):
        async with self.rwlock.r:
            self.rwlock.b += 1
            if self.rwlock.b == 1:
                await self.rwlock.g.acquire()
                # Don't worry, I known what I'm doing...
                self.rwlock.g._owner = 42

    async def __aexit__(self, *args):
        async with self.rwlock.r:
            self.rwlock.b -= 1
            if self.rwlock.b == 0:
                # Don't worry, I known what I'm doing...
                self.rwlock.g._owner = trio._core.current_task()
                self.rwlock.g.release()


@attr.s(slots=True)
class WriteLockManager:

    rwlock = attr.ib()

    async def __aenter__(self):
        await self.rwlock.g.acquire()

    async def __aexit__(self, *args):
        self.rwlock.g.release()


@attr.s(slots=True)
class RWLock:
    r = attr.ib(default=attr.Factory(trio.Lock))
    g = attr.ib(default=attr.Factory(trio.Lock))
    b = attr.ib(default=0)

    def acquire_read(self):
        return ReadLockManager(self)

    def acquire_write(self):
        return WriteLockManager(self)

The codebase is really straightforward, except for the fact the g lock is released from a different coroutine that it was acquired in the first place. This obliged me to hack the g._owner field...

This make me wonder if it wouldn't be better to have this kind part of trio directly (given no normal user should be expected to use this kind of hack on a 3rd party dependency). Beside it's a fairly common kind of lock so DRY should apply here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review related issue #19 and the proposed ReadLockManager, WriteLockManager, and RWLock implementation in this issue. Determine the supported API and concurrency behavior for a Trio read-write lock, including how it avoids the private _owner hack. Done means the project has an agreed, maintainable read-write lock approach.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.