This code provides a mixin and decorator which, when used together, can provide advisory locking on model methods. It provides locking by using MySQL's advisory lock system. See the example at the bottom of the code.



This is a convenient and easy way to guarantee your model methods have exclusive access to a model instance.

The LockableObjects class requires a MySQL backend, but it could be modified to use other back-end locking systems.



The lock name generation in LockableObject.get_lock_name() could be altered to create much more complex locking schemes. Locking per-object, per-method for example..

Lock attempts have a timeout value of 45 seconds by default. If a timeout occurs, EnvironmentError is raised.

See the bottom of the script for an example

Instructions: