FAQ
Hi all,

I currently find myself needing a Python read-write lock. I note that
there is none in the standard library, but googling "python read-write
lock" quickly produced 6 different competing examples, including two
languishing patch proposals for the standard library.

I can always pick a random one and hope for the best, but I was hoping
someone here might have a tip for one that has been used and debugged
and is likely to work.

Regards,
Geoff Bache

Search Discussions

  • Jean-Michel Pichavant at Apr 28, 2011 at 2:28 pm

    Geoff Bache wrote:
    Hi all,

    I currently find myself needing a Python read-write lock. I note that
    there is none in the standard library, but googling "python read-write
    lock" quickly produced 6 different competing examples, including two
    languishing patch proposals for the standard library.

    I can always pick a random one and hope for the best, but I was hoping
    someone here might have a tip for one that has been used and debugged
    and is likely to work.

    Regards,
    Geoff Bache
    What about

    http://docs.python.org/library/threading.html#lock-objects

    ?

    JM
  • Geoff Bache at Apr 28, 2011 at 5:14 pm

    On Thu, Apr 28, 2011 at 4:28 PM, Jean-Michel Pichavant wrote:
    Geoff Bache wrote:
    Hi all,

    I currently find myself needing a Python read-write lock. I note that
    there is none in the standard library, but googling "python read-write
    lock" quickly produced 6 different competing examples, including two
    languishing patch proposals for the standard library.

    I can always pick a random one and hope for the best, but I was hoping
    someone here might have a tip for one that has been used and debugged
    and is likely to work.

    Regards,
    Geoff Bache
    What about

    http://docs.python.org/library/threading.html#lock-objects
    Those aren't read-write locks. They are basic locks, which don't
    distinguish between readers and writers. I need to be able to lock
    between reader and writer operations, without readers locking each
    other out.

    /Geoff
  • D'Arcy J.M. Cain at Apr 28, 2011 at 5:53 pm

    On Thu, 28 Apr 2011 19:14:45 +0200 Geoff Bache wrote:
    On Thu, Apr 28, 2011 at 4:28 PM, Jean-Michel Pichavant
    wrote:
    Those aren't read-write locks. They are basic locks, which don't
    distinguish between readers and writers. I need to be able to lock
    between reader and writer operations, without readers locking each
    other out.
    There really isn't any such thing as "read-write" locks. In fact, the
    term "locks" without any further explanation is pretty loose in any
    case. You can use the above method to implement your own locking
    scheme. However, that only works if your application is a single,
    multi-threaded app. Is that the case? When I saw your original
    question I thought that you needed something inter-process. If so then
    you need another scheme.

    I think you have to first clarify your requirements and ask again. I
    have gleaned a little more information from your above response, you
    don't require readers to be locked out during an update if I understand
    the requirement. There is still a lot of information missing. For
    example:

    - What is being read and written? Files?
    - Is this a single multi-threaded app or multiple processes?
    - if multiple processes, can they be counted on to cooperate?
    - Do update requests need to be queued or is random availability OK?

    Now, don't just answer the above questions. Try to start with that and
    think about what other requirements you have. The more information you
    give us, the better the answer will be. You may even find that locking
    is not the actual answer you need. People do have a tendency to offer
    solutions when they think they are asking questions. Don't pre-suppose
    the answer.

    http://www.catb.org/~esr/faqs/smart-questions.html is always a good
    read in these situations.

    --
    D'Arcy J.M. Cain <darcy at druid.net> | Democracy is three wolves
    http://www.druid.net/darcy/ | and a sheep voting on
    +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
  • Geoff Bache at Apr 28, 2011 at 8:26 pm

    On Thu, Apr 28, 2011 at 7:53 PM, D'Arcy J.M. Cain wrote:
    On Thu, 28 Apr 2011 19:14:45 +0200
    Geoff Bache wrote:
    On Thu, Apr 28, 2011 at 4:28 PM, Jean-Michel Pichavant
    wrote:
    Those aren't read-write locks. They are basic locks, which don't
    distinguish between readers and writers. I need to be able to lock
    between reader and writer operations, without readers locking each
    other out.
    There really isn't any such thing as "read-write" locks.
    Did you google it? I even provided the exact search terms to use in my
    initial posting. It takes you straight to two standard library patch
    proposals and four other solutions, all labelled this way (sometimes
    "reader-writer locks" admittedly). That doesn't happen if it isn't a
    standard problem.

    I'm not looking for general advice on how to solve some specific
    problem. I'm asking if anyone knows anything about the relative merits
    of the above 6 solutions.

    Regards,
    Geoff Bache
  • Ben Finney at Apr 28, 2011 at 9:29 pm

    Geoff Bache <geoff.bache at gmail.com> writes:

    I currently find myself needing a Python read-write lock.
    Please tell us what you mean by ?read-write lock?.
    I note that there is none in the standard library, but googling
    "python read-write lock" quickly produced 6 different competing
    examples, including two languishing patch proposals for the standard
    library.
    That's probably because the term isn't well defined. What is your
    definition?

    --
    \ ?If you can't beat them, arrange to have them beaten.? ?George |
    `\ Carlin |
    _o__) |
    Ben Finney
  • Ryan Kelly at Apr 28, 2011 at 10:38 pm

    On Thu, 2011-04-28 at 07:02 -0700, Geoff Bache wrote:
    Hi all,

    I currently find myself needing a Python read-write lock. I note that
    there is none in the standard library, but googling "python read-write
    lock" quickly produced 6 different competing examples, including two
    languishing patch proposals for the standard library.

    I can always pick a random one and hope for the best, but I was hoping
    someone here might have a tip for one that has been used and debugged
    and is likely to work.
    I wrote and have used the "SHLock" class in threading2 which should do
    what you need. Don't know about "likely to work" but if it doesn't, I'd
    like to hear about it so I can fix it :-)

    `pip install threading2`


    Cheers,

    Ryan


    --
    Ryan Kelly
    http://www.rfk.id.au | This message is digitally signed. Please visit
    ryan at rfk.id.au | http://www.rfk.id.au/ramblings/gpg/ for details

    -------------- next part --------------
    A non-text attachment was scrubbed...
    Name: signature.asc
    Type: application/pgp-signature
    Size: 198 bytes
    Desc: This is a digitally signed message part
    URL: <http://mail.python.org/pipermail/python-list/attachments/20110429/b02f33bc/attachment.pgp>
  • Geoff Bache at Apr 29, 2011 at 3:05 pm

    On Fri, Apr 29, 2011 at 12:38 AM, Ryan Kelly wrote:
    On Thu, 2011-04-28 at 07:02 -0700, Geoff Bache wrote:
    Hi all,

    I currently find myself needing a Python read-write lock. I note that
    there is none in the standard library, but googling "python read-write
    lock" quickly produced 6 different competing examples, including two
    languishing patch proposals for the standard library.

    I can always pick a random one and hope for the best, but I was hoping
    someone here might have a tip for one that has been used and debugged
    and is likely to work.
    I wrote and have used the "SHLock" class in threading2 which should do
    what you need. ?Don't know about "likely to work" but if it doesn't, I'd
    like to hear about it so I can fix it :-)
    That's good enough for me :) Thanks, I'll give it a try.

    /Geoff

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 28, '11 at 2:02p
activeApr 29, '11 at 3:05p
posts8
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase