Read-write lock for Python

D'Arcy J.M. Cain darcy at druid.net
Thu Apr 28 13:53:58 EDT 2011


On Thu, 28 Apr 2011 19:14:45 +0200
Geoff Bache <geoff.bache at gmail.com> wrote:
> On Thu, Apr 28, 2011 at 4:28 PM, Jean-Michel Pichavant
> <jeanmichel at 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.  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.



More information about the Python-list mailing list