如何从特定的django信号接收器获得结果?

时间:2015-06-22 18:48:28

标签: python django signals django-signals

我发现了这个问题,但没有看到任何好的答案:Getting information from Django custom signal receiver

这是我的问题:

在send_signals_and_get_transition_status()函数内部我发送2个信号但是如果第一个信号接收器的check_if_server_connected()响应为False,我不想执行另一个信号。我如何得到这个特定接收器的响应?我在django文档中找不到它。

我不能像响应[0] [1]那样得到它,因为响应列表中结果的顺序可能不同。

signals.py:

waiting_for_auto_connect_task = Signal(providing_args=["server_id"])

waiting_for_server_success_tasks = Signal(providing_args=["server_id"])

__ INIT __ PY:

def send_signals_and_get_transition_status(server_id):

    response = wait_for_auto_connect_task.send(sender=None, server_id=server_id)

    logger.debug('First signal response: {}'.format(response))

    # Get results from the receiver check_if_server_connected() response
    # If response it True only then send another signal

        response = waiting_for_server_success_tasks.send(sender=None, server_id=server_id)

    # else:
        raise CouldNotConnectToTheServerException()


@receiver(wait_for_auto_connect_task)
def check_if_server_connected(sender, **kwargs):
    server_id = kwargs["server_id"]

    # ...

    results = check_if_server_connected_task.apply_async((server_id,))
    return results.get()


@receiver(wait_for_auto_connect_task)
def check_if_all_services_are_ready(sender, **kwargs):
    server_id = kwargs["server_id"]

    # ...

    return are_services_ready(server_id)

第一个信号响应:

[(<function check_if_server_connected at 0x6875de8>, False), (<function check_if_all_services_are_ready at 0x6875e60>, True)]

1 个答案:

答案 0 :(得分:0)

我想我自己弄清楚了。不确定这是否是最好的解决方案,但我做了一个能让我得到结果的函数:

def get_results_from_signal_receiver(response, receiver_function_name):
    for i in response:
        if i[0].__name__ == receiver_function_name:
            return i[1]
    return None