Read-write lock for Python

G

Geoff Bache

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
 
J

Jean-Michel Pichavant

Geoff said:
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
 
D

D'Arcy J.M. Cain

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.
 
G

Geoff Bache

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
 
R

Ryan Kelly

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
(e-mail address removed) | http://www.rfk.id.au/ramblings/gpg/ for details


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iEYEABECAAYFAk257FAACgkQfI5S64uP50oFNQCgqd1WkBy4hXDo46lVeagmlGYQ
QeIAnjstAa/a8/O/2/G1/7XJI5AUb85N
=8yAH
-----END PGP SIGNATURE-----
 
G

Geoff Bache

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top