Distributed locking

Diez B. Roggisch deets at nospam.web.de
Thu May 14 09:34:23 CEST 2009

James schrieb:
> Hey all, I'm looking for suggestions on how to tackle distributed
> locking across several Python programs on several different machines.
> - the objects to be locked are uniquely identified by an integer
> - I need "one at a time" semantics for the lock: zero or one read-
> writer at any point
> - the operations to be performed on the objects are strictly limited
> in duration (no more than 10 seconds)
> As the operations have a maximum duration, my ideal solution would be
> to lock the objects, and have them automatically unlock after some
> time period even if the original lock-holder doesn't release them.
> I'm currently playing with memcached as a locking mechanism as it's
> simplicity and timeout fit my requirements well.
> There is a check-and-set operation which atomically writes new data
> iff the existing value hasn't been changed since we last looked.
> However, their CAS operation doesn't handle the case of a non-existent
> key..
> Does anyone have suggestions on how I can do distributed locking to
> take advantage of the time limit on operations?

I'd use a database. Maybe the time limit could be modeled with a 
transaction-timeout, but even if not, you should be able to model a 
solution with timestamps and distinct transactions to modify the locked row.

Also, for the case of the non-existing lock, go with something like this:

if not lock_exists(my_lock):
    if not lock_exists(my_lock):

with my_lock:

In the __enter__ and __exit__-methods of my_lock, you can perfom the 
necessary database-ops.


More information about the Python-list mailing list