1
# -*- coding: iso-8859-1 -*-
5
# synchronized objects and methods.
7
# From http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65202
12
def _get_method_names (obj):
13
if type(obj) == InstanceType:
14
return _get_method_names(obj.__class__)
16
elif type(obj) == ClassType:
18
for name, func in obj.__dict__.items():
19
if type(func) == FunctionType:
20
result.append((name, func))
22
for base in obj.__bases__:
23
result.extend(_get_method_names(base))
28
class _SynchronizedMethod:
30
def __init__ (self, method, obj, lock):
31
self.__method = method
35
def __call__ (self, *args, **kwargs):
38
#print 'Calling method %s from obj %s' % (self.__method, self.__obj)
39
return self.__method(self.__obj, *args, **kwargs)
43
class SynchronizedObject:
44
def __init__ (self, obj, ignore=[], lock=None):
49
lock = lock and lock or threading.RLock()
50
for name, method in _get_method_names(obj):
51
if not name in ignore:
52
self.__methods[name] = _SynchronizedMethod(method, obj, lock)
54
def __getattr__ (self, name):
56
return self.__methods[name]
58
return getattr(self.__obj, name)