diff --git a/src/py/flwr/client/app.py b/src/py/flwr/client/app.py index 00cb56d6579..7e521472676 100644 --- a/src/py/flwr/client/app.py +++ b/src/py/flwr/client/app.py @@ -34,6 +34,7 @@ def start_client( client: Client, grpc_max_message_length: int = GRPC_MAX_MESSAGE_LENGTH, root_certificates: Optional[bytes] = None, + wait_for_ready: bool = False, ) -> None: """Start a Flower Client which connects to a gRPC server. @@ -55,6 +56,8 @@ class `flwr.client.Client`. The PEM-encoded root certificates as a byte string. If provided, a secure connection using the certificates will be established to a SSL-enabled Flower server. + wait_for_ready: bool (default: False). If set to True, the client does not + fail fast, but waits until a connection to the server can be established. Returns ------- @@ -84,6 +87,7 @@ class `flwr.client.Client`. server_address, max_message_length=grpc_max_message_length, root_certificates=root_certificates, + wait_for_ready=wait_for_ready, ) as conn: receive, send = conn @@ -112,6 +116,7 @@ def start_numpy_client( client: NumPyClient, grpc_max_message_length: int = GRPC_MAX_MESSAGE_LENGTH, root_certificates: Optional[bytes] = None, + wait_for_ready: bool = False, ) -> None: """Start a Flower NumPyClient which connects to a gRPC server. @@ -133,6 +138,8 @@ class `flwr.client.NumPyClient`. The PEM-encoded root certificates a byte string. If provided, a secure connection using the certificates will be established to a SSL-enabled Flower server. + wait_for_ready: bool (default: False). If set to True, the client does not + fail fast, but waits until a connection to the server can be established. Returns ------- @@ -173,4 +180,5 @@ class `flwr.client.NumPyClient`. client=flower_client, grpc_max_message_length=grpc_max_message_length, root_certificates=root_certificates, + wait_for_ready=wait_for_ready, ) diff --git a/src/py/flwr/client/grpc_client/connection.py b/src/py/flwr/client/grpc_client/connection.py index 84177ee21ae..46bcafdbe0b 100644 --- a/src/py/flwr/client/grpc_client/connection.py +++ b/src/py/flwr/client/grpc_client/connection.py @@ -44,6 +44,7 @@ def grpc_connection( server_address: str, max_message_length: int = GRPC_MAX_MESSAGE_LENGTH, root_certificates: Optional[bytes] = None, + wait_for_ready: bool = False, ) -> Iterator[Tuple[Callable[[], ServerMessage], Callable[[ClientMessage], None]]]: """Establish an insecure gRPC connection to a gRPC server. @@ -64,6 +65,8 @@ def grpc_connection( The PEM-encoded root certificates as a byte string. If provided, a secure connection using the certificates will be established to a SSL-enabled Flower server. + wait_for_ready: bool (default: False). If set to True, the client does not + fail fast, but waits until a connection to the server can be established. Returns ------- @@ -108,7 +111,9 @@ def grpc_connection( ) stub = FlowerServiceStub(channel) - server_message_iterator: Iterator[ServerMessage] = stub.Join(iter(queue.get, None)) + server_message_iterator: Iterator[ServerMessage] = stub.Join( + iter(queue.get, None), wait_for_ready=wait_for_ready + ) receive: Callable[[], ServerMessage] = lambda: next(server_message_iterator) send: Callable[[ClientMessage], None] = lambda msg: queue.put(msg, block=False)