p2pfl.communication.protocols.memory.memory_communication_protocol moduleΒΆ

In-memory communication protocol.

class p2pfl.communication.protocols.memory.memory_communication_protocol.InMemoryCommunicationProtocol(addr=None, commands=None)[source]ΒΆ

Bases: CommunicationProtocol

In-memory communication protocol.

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

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

Todo

Remove this copy-paste code and use a in-memory wrapper for the grpc communication protocol.

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 (Dict[str, Union[str, int, bytes, List[str]]]) – 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 (Dict[str, Union[str, int, bytes, List[str]]]) – The message to sen

  • raise_error (bool) – If raise error.

  • remove_on_error (bool) – If remove on error.d.

Return type:

None

start()[source]ΒΆ

Start the communication protocol.

Return type:

None

stop()[source]ΒΆ

Stop the communication protocol.

Return type:

None

wait_for_termination()[source]ΒΆ

Wait for termination.

Return type:

None

p2pfl.communication.protocols.memory.memory_communication_protocol.running(func)[source]ΒΆ

Ensure that the server is running before executing a method.