Synchronizing methods of a class

K

Keith Veleba

Hello to all fellow c.l.p'ers!

Long-time listener, first-time caller.

Background:
I'm working on a project where I have to do some serious
multithreading. I've worked up a decorator in Python 2.3.4 to implement
the lock semantics required for specific functions I want to
synchronize:

def synchronized(method):
def f(*args,**kwargs):
self = args[0]
self._lock.acquire();
try:
return method(*args,**kwargs)
finally:
self._lock.release()
return f

And a convenience method to synchronize all methods of a given class:

def synchronize(klass, allow=None, deny=None):
"""Synchronize methods in the given class
Only synchronize the methods whose names are
not in the deny list,in the allow list, or all methods if allow and
deny are not specified.."""
if deny is None: deny=[]
deny.append('__init__')
for (name, val) in klass.__dict__.items():
print "attr '%s':" % (name),
if callable(val):
if name not in deny:
if allow is None or name in allow:
klass.__dict__[name] = synchronized(val)
print "synchronized."
else:
print "not synchronizing, name not in allow list" % name
else:
print "not synchronizing, name in deny list."
else:
print "not synchronizing, not callable."
return klass

Obviously, my classes have to instantiate the _lock in __init__ in
order for this to work.

Problem:

When iterating through klass.__dict__.items() in the convenience
method, I only get the instance variables of the class. No functions.
I've found a way to get the function list, by iterating through
klass.__class__.__dict__ .

My Question:

If I decorate these function references in __class__.__dict__, am I
doing it only for my specific instance of that class or the base class
as well?

Thanks in advance,
Keith Veleba
 
D

Diez B. Roggisch

My Question:
If I decorate these function references in __class__.__dict__, am I
doing it only for my specific instance of that class or the base class
as well?

A class is a class - so yes, all instances are affected. You might want to
think about metaclasses for decorating.
 
C

Christopher De Vries

Background:
I'm working on a project where I have to do some serious
multithreading. I've worked up a decorator in Python 2.3.4 to implement
the lock semantics required for specific functions I want to
synchronize:

I found Chris Liechti's example very helpful when working on a similar
project. See
http://groups-beta.google.com/group/comp.lang.python/msg/d647a830de39d1db
.. Also, I strongly suggest using threading.RLock() objects instead of primitive
locks. With Rlocks one thread can acquire the same lock multiple times without
blocking. This is useful if one synchronized method calls another synchronized method.
Obviously, my classes have to instantiate the _lock in __init__ in
order for this to work.

The great thing about Chris's example is that the first time a synchronized
method is called a lock is instantiated, so you don't have to worry about this
step.
Problem:

When iterating through klass.__dict__.items() in the convenience
method, I only get the instance variables of the class. No functions.
I've found a way to get the function list, by iterating through
klass.__class__.__dict__ .

I'm not sure why klass.__dict__.items() isn't working for you. I tried a simple
example:

class simple(object):
def hello(self):
print "Hello World"

def methods(cls):
for name,value in cls.__dict__.items():
if callable(value):
print name
hello


Chris
 
K

Keith Veleba

Chris,

.... def __init__(self):
.... threading.Thread.__init__(self)
.... def one(self):
.... pass
.... def two(self):
.... pass
....[('_Thread__block',
<Condition(<thread.lock object at 0x009BE070>, 0)>),
('_Thread__name', 'Thread-1'),
('_Thread__daemonic', False),
('_Thread__started', False),
('_Thread__target', None),
('_Thread__kwargs', {}),
('_Verbose__verbose', False),
('_Thread__args', ()),
('_Thread__stopped', False),
('_Thread__initialized', True)]

Neither function I added to the A class shows up.

However, I think it's because I'm using an instance of my class vs.
just referencing the class type.

If I type:

A.__dict__items()

I get the correct list:

[('__module__', '__main__'),
('__doc__', None),
('two', <function two at 0x009E21B0>),
('__init__', <function __init__ at 0x009E2170>),
('one', <function one at 0x009E2230>)]

In any case, thanks for the example reference. That's helps me improve
my idea, and I will most likely use the methods in it.

Keith
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top