Metaclasses, decorators, and synchronization

Tom Anderson twic at
Mon Sep 26 21:16:01 CEST 2005

On Mon, 26 Sep 2005, Jp Calderone wrote:

> On Sun, 25 Sep 2005 23:30:21 -0400, Victor Ng <crankycoder at> wrote:
>> You could do it with a metaclass, but I think that's probably overkill.
>> It's not really efficient as it's doing test/set of an RLock all the
>> time, but hey - you didn't ask for efficient.  :)
> There's a race condition in this version of synchronized which can allow two 
> or more threads to execute the synchronized function simultaneously.

You could define a meta-lock, and use that to protect the 
lock-installation action.

To avoid bottlenecking on the single meta-lock, you could put a meta-lock 
in each class, and use that to protect installation of locks in instances 
of that class. You would, of course, then need a meta-meta-lock to protect 

Also, and more helpfully, you can get a modest speedup by taking out the 
explicit test for the presence of the lock, and just rely on getting an 
AttributeError from self._sync_lock.acquire() if it's not there; you could 
then install the lock in the except suite.


90% mental, 25% effort, 8% mathematics

More information about the Python-list mailing list