2015-06-22 64 views
0

我發現這個問題,但並沒有看到什麼好的答案來:Getting information from Django custom signal receiver如何從特定的django信號接收器獲得結果?

這裏是我的問題:

內send_signals_and_get_transition_status()函數,我送2個信號,但如果第一信號接收器的check_if_server_connected()響應假我不想執行另一個信號。我如何得到這個特定接收器的響應?我無法在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)] 

回答

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