p2pfl.communication.protocols.grpc.grpc_communication_protocol moduleΒΆ

GRPC communication protocol.

class p2pfl.communication.protocols.grpc.grpc_communication_protocol.GrpcCommunicationProtocol(addr='127.0.0.1', commands=None)[source]ΒΆ

Bases: CommunicationProtocol

GRPC communication protocol.

Parameters:
  • addr (str) – Address of the node.

  • commands (Optional[List[Command]]) – Commands to add to the communication protocol.

Todo

Decouple the heeartbeat command.

add_command(cmds)[source]ΒΆ

Add a command to the communication protocol.

Parameters:

cmds (Union[Command, List[Command]]) – The command to add.

Return type:

None

broadcast(msg, node_list=None)[source]ΒΆ

Broadcast a message to all neighbors.

Parameters:
  • msg (RootMessage) – The message to broadcast.

  • node_list (Optional[List[str]]) – Optional node list.

Return type:

None

build_msg(cmd, args=None, round=None)[source]ΒΆ

Build a message.

Parameters:
  • cmd (str) – The message.

  • args (Optional[List[str]]) – The arguments.

  • round (Optional[int]) – The round.

Return type:

Any

build_weights(cmd, round, serialized_model, contributors=None, weight=1)[source]ΒΆ

Build weights.

Parameters:
  • cmd (str) – The command.

  • round (int) – The round.

  • serialized_model (bytes) – The serialized model.

  • contributors (Optional[List[str]]) – The model contributors.

  • weight (int) – The weight of the model (amount of samples used).

Return type:

Any

connect(addr, non_direct=False)[source]ΒΆ

Connect to a neighbor.

Parameters:
  • addr (str) – The address to connect to.

  • non_direct (bool) – The non direct flag.

Return type:

bool

disconnect(nei, disconnect_msg=True)[source]ΒΆ

Disconnect from a neighbor.

Parameters:
  • nei (str) – The neighbor to disconnect from.

  • disconnect_msg (bool) – The disconnect message flag.

Return type:

None

get_address()[source]ΒΆ

Get the address.

Return type:

str

Returns:

The address.

get_neighbors(only_direct=False)[source]ΒΆ

Get the neighbors.

Parameters:

only_direct (bool) – The only direct flag.

Return type:

Dict[str, Any]

gossip_weights(early_stopping_fn, get_candidates_fn, status_fn, model_fn, period=None, create_connection=False)[source]ΒΆ

Gossip model weights.

Parameters:
  • early_stopping_fn (Callable[[], bool]) – The early stopping function.

  • get_candidates_fn (Callable[[], List[str]]) – The get candidates function.

  • status_fn (Callable[[], Any]) – The status function.

  • model_fn (Callable[[str], Any]) – The model function.

  • period (Optional[float]) – The period.

  • create_connection (bool) – The create connection flag.

Return type:

None

send(nei, msg, raise_error=False, remove_on_error=True)[source]ΒΆ

Send a message to a neighbor.

Parameters:
  • nei (str) – The neighbor to send the message.

  • msg (RootMessage) – The message to send.

  • raise_error (bool) – If raise error.

  • remove_on_error (bool) – If remove on error.

Return type:

None

start()[source]ΒΆ

Start the GRPC communication protocol.

Return type:

None

stop()[source]ΒΆ

Stop the GRPC communication protocol.

Return type:

None

wait_for_termination()[source]ΒΆ

Get the neighbors.

Parameters:

only_direct – The only direct flag.

Return type:

None

p2pfl.communication.protocols.grpc.grpc_communication_protocol.running(func)[source]ΒΆ

Ensure that the server is running before executing a method.