Python 调用正在忽略方法修饰符

Python 调用正在忽略方法修饰符,python,twisted,python-decorators,Python,Twisted,Python Decorators,我正在与的@synchronized装饰器一起使用。当PyDispatcher调用我的一个处理程序方法时,它忽略了函数decorator 我已经为此创建了一个测试用例。运行以下代码会同时运行两个线程,但预期的行为是,@synchronized装饰程序应该阻止第二个线程进入\u method\u b方法,直到\u method\u a完成: from wrapt.decorators import synchronized import time from twisted.internet imp

我正在与的
@synchronized
装饰器一起使用。当PyDispatcher调用我的一个处理程序方法时,它忽略了函数decorator

我已经为此创建了一个测试用例。运行以下代码会同时运行两个线程,但预期的行为是,
@synchronized
装饰程序应该阻止第二个线程进入
\u method\u b
方法,直到
\u method\u a
完成:

from wrapt.decorators import synchronized
import time
from twisted.internet import reactor, threads
from twisted.internet.defer import inlineCallbacks
from pydispatch import dispatcher

class TestSync(object):

    def __init__(self):
        dispatcher.connect(self._method_a, signal="A")
        dispatcher.connect(self._method_b, signal="B")

    @synchronized
    def _method_a(self):
        print "Before Sleep A"
        time.sleep(2)
        print "After Sleep A"

    @synchronized
    def _method_b(self):
        print "Before Sleep B"
        time.sleep(2)
        print "After Sleep B"

    def method_a(self):
        self._method_a()

    def method_b(self):
        self._method_b()

    def test(self):
        reactor.callInThread(dispatcher.send, signal="A")
        reactor.callInThread(dispatcher.send, signal="B")

ts = TestSync()
reactor.callLater(0, ts.test)
reactor.run()
但是,如果不是从Pydispatch调用这些方法,而是从另一个方法调用它们,
@synchronized
装饰器确实可以按预期工作。这可以通过使用
方法a
方法b
方法进行测试,这两种方法依次称为修饰方法:

def __init__(self):
    dispatcher.connect(self.method_a, signal="A")
    dispatcher.connect(self.method_b, signal="B")

这是预期的行为吗?我怎样才能告诉Pydispatch不要忽略装饰器呢?

我不太熟悉,但是
@synchronized
是否适用于函数内部或跨函数的内容?从一个局外人的角度来看,如果我有两个方法,每个方法都需要一个同步的主体,如果这有意义的话,我不一定希望/期望方法本身被同步执行。同样,我不熟悉这个decorator或
PyDispatch
,因此如果这是一个无意义的注释,我很抱歉。默认情况下,它使用对象实例上的RLock,因此在本例中它跨方法应用,但锁本身存储为实例属性。这是可行的,除了当PyDispatcher.FWIW调用时,问题可能在于PyDispatcher在尝试调用回调函数时似乎做了一些可怕的事情。可疑代码可在
pydispatch.robustapply.function()
中找到。不知道他们试图做什么,但似乎绕过了调用Python实例方法的正常方式,绕过Python描述符协议,直接获取
im_func
\uu func
,因此导致
同步的
装饰程序不能像绑定那样正常工作。所以PyDispatcher包似乎出了问题。我已经检查了
robustapply
,注意到了奇怪的健壮性,但我自己不敢尝试修复,因为我不太明白为什么会这样调用方法。谢谢你的洞察力。