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
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
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.
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
> 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
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
That's good enough for me :) Thanks, I'll give it a try.
/Geoff