From 111eecc6231f0e7010d6a42e01db38dc475cf019 Mon Sep 17 00:00:00 2001 From: Charles Beauville Date: Sun, 9 Jun 2024 17:08:45 +0200 Subject: [PATCH] refactor(framework:skip) Add no-op indent level to `_start_client_internal` (#3564) --- src/py/flwr/client/app.py | 155 +++++++++++++++++++------------------- 1 file changed, 79 insertions(+), 76 deletions(-) diff --git a/src/py/flwr/client/app.py b/src/py/flwr/client/app.py index 4e09c53c2b00..eb07a732439d 100644 --- a/src/py/flwr/client/app.py +++ b/src/py/flwr/client/app.py @@ -327,90 +327,93 @@ def _on_backoff(retry_state: RetryState) -> None: create_node() # pylint: disable=not-callable while True: - # Receive - message = receive() - if message is None: - time.sleep(3) # Wait for 3s before asking again - continue - - log(INFO, "") - if len(message.metadata.group_id) > 0: + if True: # pylint: disable=using-constant-test + # Receive + message = receive() + if message is None: + time.sleep(3) # Wait for 3s before asking again + continue + + log(INFO, "") + if len(message.metadata.group_id) > 0: + log( + INFO, + "[RUN %s, ROUND %s]", + message.metadata.run_id, + message.metadata.group_id, + ) log( INFO, - "[RUN %s, ROUND %s]", - message.metadata.run_id, - message.metadata.group_id, + "Received: %s message %s", + message.metadata.message_type, + message.metadata.message_id, ) - log( - INFO, - "Received: %s message %s", - message.metadata.message_type, - message.metadata.message_id, - ) - - # Handle control message - out_message, sleep_duration = handle_control_message(message) - if out_message: - send(out_message) - break - - # Register context for this run - node_state.register_context(run_id=message.metadata.run_id) - - # Retrieve context for this run - context = node_state.retrieve_context(run_id=message.metadata.run_id) - - # Create an error reply message that will never be used to prevent - # the used-before-assignment linting error - reply_message = message.create_error_reply( - error=Error(code=ErrorCode.UNKNOWN, reason="Unknown") - ) - - # Handle app loading and task message - try: - # Load ClientApp instance - client_app: ClientApp = load_client_app_fn() - - # Execute ClientApp - reply_message = client_app(message=message, context=context) - except Exception as ex: # pylint: disable=broad-exception-caught - - # Legacy grpc-bidi - if transport in ["grpc-bidi", None]: - log(ERROR, "Client raised an exception.", exc_info=ex) - # Raise exception, crash process - raise ex - - # Don't update/change NodeState - - e_code = ErrorCode.CLIENT_APP_RAISED_EXCEPTION - # Reason example: ":<'division by zero'>" - reason = str(type(ex)) + ":<'" + str(ex) + "'>" - exc_entity = "ClientApp" - if isinstance(ex, LoadClientAppError): - reason = ( - "An exception was raised when attempting to load " - "`ClientApp`" - ) - e_code = ErrorCode.LOAD_CLIENT_APP_EXCEPTION - exc_entity = "SuperNode" - log(ERROR, "%s raised an exception", exc_entity, exc_info=ex) + # Handle control message + out_message, sleep_duration = handle_control_message(message) + if out_message: + send(out_message) + break - # Create error message - reply_message = message.create_error_reply( - error=Error(code=e_code, reason=reason) + # Register context for this run + node_state.register_context(run_id=message.metadata.run_id) + + # Retrieve context for this run + context = node_state.retrieve_context( + run_id=message.metadata.run_id ) - else: - # No exception, update node state - node_state.update_context( - run_id=message.metadata.run_id, - context=context, + + # Create an error reply message that will never be used to prevent + # the used-before-assignment linting error + reply_message = message.create_error_reply( + error=Error(code=ErrorCode.UNKNOWN, reason="Unknown") ) - # Send - send(reply_message) - log(INFO, "Sent reply") + # Handle app loading and task message + try: + # Load ClientApp instance + client_app: ClientApp = load_client_app_fn() + + # Execute ClientApp + reply_message = client_app(message=message, context=context) + except Exception as ex: # pylint: disable=broad-exception-caught + + # Legacy grpc-bidi + if transport in ["grpc-bidi", None]: + log(ERROR, "Client raised an exception.", exc_info=ex) + # Raise exception, crash process + raise ex + + # Don't update/change NodeState + + e_code = ErrorCode.CLIENT_APP_RAISED_EXCEPTION + # Ex fmt: ":<'division by zero'>" + reason = str(type(ex)) + ":<'" + str(ex) + "'>" + exc_entity = "ClientApp" + if isinstance(ex, LoadClientAppError): + reason = ( + "An exception was raised when attempting to load " + "`ClientApp`" + ) + e_code = ErrorCode.LOAD_CLIENT_APP_EXCEPTION + exc_entity = "SuperNode" + + log(ERROR, "%s raised an exception", exc_entity, exc_info=ex) + + # Create error message + reply_message = message.create_error_reply( + error=Error(code=e_code, reason=reason) + ) + else: + # No exception, update node state + node_state.update_context( + run_id=message.metadata.run_id, + context=context, + ) + + # Send + send(reply_message) + log(INFO, "Sent reply") # Unregister node if delete_node is not None: