How can I get the results from the particular django-signals receiver?

399 views Asked by At

I found this question but did not see any good answer to it: Getting information from Django custom signal receiver

Here is my problem:

Inside send_signals_and_get_transition_status() function I send 2 signals but in case first signal receiver's check_if_server_connected() response is False I don't want to execute another signal. How do i get a response from this particular receiver? I could not find it in django documentation.

I cannot get it like this response[0][1] as the order of the results in the response list might be different.

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)

First signal response:

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

There are 1 answers

0
DNWS On

I think I figured it out myself. Not sure if this is the best solution, but I made a function which gets me the results:

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