I am trying to create an @synchronized wrapper that creates one Lock for each object and makes method calls thread safe. I can only do this if I can access the .im_self method of the method in the wrapped method.
class B: def f(self): pass assert inspect.ismethod( Bf )
(1) What is confusing is that the func parameter passed to my decorator changes type before it is passed to the wrapper generator. It seems rude and unnecessary. Why is this happening?
(2) Is there some kind of decorator sorceress with which I can make method calls on a mutex-ed object (i.e. one lock for each object, not a class).
UPDATE: There are many examples of @synchronized (lock) wrappers. However, really, I want @synchronized (self). I can solve it like this:
def synchronizedMethod(func): def _synchronized(*args, **kw): self = args[0] lock = oneLockPerObject(self) with lock: return func(*args, **kw) return _synchronized
However, since it is much more efficient, I would prefer:
def synchronizedMethod(func): lock = oneLockPerObject(func.im_self) def _synchronized(*args, **kw): with lock: return func(*args, **kw) return _synchronized
Is it possible?
source share