Locking access to all data members

Diez B. Roggisch deets at nospam.web.de
Sun Jan 28 15:08:12 CET 2007

gooli schrieb:
> I have a class with a lot of attributes whose objects are accessed 
> from multiple threads. I would like to synchronize the access to all 
> the attributes, i.e. acquire a lock, return the value, release the 
> lock (in a finally clause).
> Is there a way to do that without turning each attribute into a 
> property and manually wrapping each property with the locking code?
> How about classes bound with SQLAlchemy? Is it possible to do that for 
> those too?
> Example of a class I would like to wrap:
> class Job(object):
>     def __init__(self, itemType):
>         self.id = self.idSequence
>         self.itemType = itemType
>         self.status = "waiting"
>         self.createDate = None
>         self.hostName = None
>         self.progress = 0
>         self.items = []

You can use a metaclass or decorators to put the locking code in place 
for all methods. I'm not sure though if that interferes with SQLAlchemy.


More information about the Python-list mailing list