By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

Synchronizing methods of a class

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

P: n/a
On Mon, Feb 07, 2005 at 11:57:02AM -0800, Keith Veleba wrote:
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/...47a830de39d1db
.. 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
methods(simple)

hello
Chris
Jul 18 '05 #3

P: n/a
Chris,

Q&D example of <<class>>.__dict__.items() not working for me:
import threading
class A(threading.Thread): .... def __init__(self):
.... threading.Thread.__init__(self)
.... def one(self):
.... pass
.... def two(self):
.... pass
.... a = A()
a.__dict__.items()

[('_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

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.