【发布时间】:2015-04-25 19:42:21
【问题描述】:
我知道为了防止重复信号,需要加上dispatch_uid(from django documentation)
但我注意到,有时当我将两个以上的接收器连接到同一个信号(没有 uid)时,只会调用其中一个。
当我尝试向其中一个添加调度 uid 时,两者都被调用。
是什么原因?
谢谢
【问题讨论】:
标签: python django django-signals
我知道为了防止重复信号,需要加上dispatch_uid(from django documentation)
但我注意到,有时当我将两个以上的接收器连接到同一个信号(没有 uid)时,只会调用其中一个。
当我尝试向其中一个添加调度 uid 时,两者都被调用。
是什么原因?
谢谢
【问题讨论】:
标签: python django django-signals
如果你看一下发送方法源代码:
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,信号都应该发送给所有接收器,所以我猜测您要么不正确地连接了一个接收器,要么其中一个在执行期间引发了错误。
【讨论】: