p2pfl.communication.protocols.communication_protocol moduleΒΆ

Communication protocol.

class p2pfl.communication.protocols.communication_protocol.CommunicationProtocol(addr='address', commands=None)[source]ΒΆ

Bases: ABC

Communication protocol interface.

Parameters:
  • addr (str) – The address.

  • commands (Optional[List[Command]]) – The commands.

abstract add_command(cmds)[source]ΒΆ

Add a command to the communication protocol.

Parameters:

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

Return type:

None

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

Broadcast a message to all neighbors.

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

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

Return type:

None

abstract 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:

str

abstract 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

abstract 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

abstract 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

abstract get_address()[source]ΒΆ

Get the address.

Return type:

str

Returns:

The address.

abstract get_neighbors(only_direct=False)[source]ΒΆ

Get the neighbors.

Parameters:

only_direct (bool) – The only direct flag.

Return type:

Dict[str, Any]

abstract 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

abstract 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 (Any) – The message to send.

  • raise_error (bool) – If raise error.

  • remove_on_error (bool) – If remove on error.

Return type:

None

abstract start()[source]ΒΆ

Start the communication protocol.

Return type:

None

abstract stop()[source]ΒΆ

Stop the communication protocol.

Return type:

None

abstract wait_for_termination()[source]ΒΆ

Wait for termination.

Return type:

None