Distributed locking

J

James

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?

Many thanks,
James
 
D

Diez B. Roggisch

James said:
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):
lock_locks_table()
if not lock_exists(my_lock):
create_lock(my_lock)

with my_lock:
...


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


Diez
 
P

Piet van Oostrum

James said:
J> Hey all, I'm looking for suggestions on how to tackle distributed
J> locking across several Python programs on several different machines.

Have you looked at the multiprocessing package? It has distributed
Locks's with timeouts which might well fit your requirements.
 

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,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top