django信号默认disptch uid值

时间:2015-02-24 08:52:19

标签: python django django-signals

我知道为了防止重复信号,需要添加dispatch_uid(from django documentation

但是我注意到有时当我将两个以上的接收器连接到同一个信号(没有uid)时,只会调用其中一个接收器。

当我尝试将调度uid添加到其中一个时,两个都被调用。

是什么原因?

感谢

1 个答案:

答案 0 :(得分:0)

如果您查看send方法源代码:

def send(self, sender, **named):
    """
    Send signal from sender to all connected receivers.
    If any receiver raises an error, the error propagates back through send,
    terminating the dispatch loop, so it is quite possible to not have all
    receivers called if a raises an error.
    Arguments:

        sender
            The sender of the signal Either a specific object or None.

        named
            Named arguments which will be passed to receivers.
    Returns a list of tuple pairs [(receiver, response), ... ].
    """
    responses = []
    if not self.receivers:
        return responses

    for receiver in self._live_receivers(_make_id(sender)):
        response = receiver(signal=self, sender=sender, **named)
        responses.append((receiver, response))
    return responses

您会注意到信号应该发送给所有接收者,关于您是否已经指定了dispatch_uid,所以我猜测您未正确连接其中一个接收器,或者其中一个发生了错误执行。