Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Read-write lock for Python

1,180 views
Skip to first unread message

Geoff Bache

unread,
Apr 28, 2011, 10:02:36 AM4/28/11
to
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

Jean-Michel Pichavant

unread,
Apr 28, 2011, 10:28:59 AM4/28/11
to Geoff Bache, pytho...@python.org

Geoff Bache

unread,
Apr 28, 2011, 1:14:45 PM4/28/11
to Jean-Michel Pichavant, pytho...@python.org
On Thu, Apr 28, 2011 at 4:28 PM, Jean-Michel Pichavant
<jeanm...@sequans.com> wrote:
> 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

unread,
Apr 28, 2011, 1:53:58 PM4/28/11
to geoff...@pobox.com, pytho...@python.org

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 <da...@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

unread,
Apr 28, 2011, 4:26:47 PM4/28/11
to D'Arcy J.M. Cain, pytho...@python.org
On Thu, Apr 28, 2011 at 7:53 PM, D'Arcy J.M. Cain <da...@druid.net> wrote:
> On Thu, 28 Apr 2011 19:14:45 +0200
> Geoff Bache <geoff...@gmail.com> wrote:
>> On Thu, Apr 28, 2011 at 4:28 PM, Jean-Michel Pichavant
>> <jeanm...@sequans.com> wrote:
>> > 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.
>
> 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

unread,
Apr 28, 2011, 5:29:24 PM4/28/11
to
Geoff Bache <geoff...@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

unread,
Apr 28, 2011, 6:38:12 PM4/28/11
to Geoff Bache, pytho...@python.org

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
ry...@rfk.id.au | http://www.rfk.id.au/ramblings/gpg/ for details

signature.asc

Geoff Bache

unread,
Apr 29, 2011, 11:05:51 AM4/29/11
to Ryan Kelly, pytho...@python.org
On Fri, Apr 29, 2011 at 12:38 AM, Ryan Kelly <ry...@rfk.id.au> wrote:
> On Thu, 2011-04-28 at 07:02 -0700, Geoff Bache wrote:
> 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

0 new messages