libmuscle.communicator module

class libmuscle.communicator.Communicator(kernel: Reference, index: list[int], port_manager: PortManager, profiler: Profiler, manager: MMPClient)[source]

Bases: object

Communication engine for MUSCLE3.

This class is the mailroom for a kernel that uses MUSCLE3. It manages the sending and receiving of messages, although it leaves the actual data transmission to various protocol-specific servers and clients.

get_locations() → list[str][source]

Returns a list of locations that we can be reached at.

These locations are of the form ‘protocol:location’, where the protocol name does not contain a colon and location may be an arbitrary string.

Returns:

A list of strings describing network locations.

get_state() → CommunicatorState[source]

Return the current internal state for checkpointing.

This includes states for the communicator, port manager and timeline manager.

pre_receive() → dict[tuple[str, int | None], Message][source]

Pre-receive on all connected F_INIT ports and S ports with repeat filters.

Returns:

The received messages on F_INIT ports (including muscle_settings_in).

receive_s_message(port_name: str, slot: int | None = None) → Message[source]

Receive a message and attached settings overlay on an “S” port.

Receiving is a blocking operation. This function will contact the sender, wait for a message to be available, and receive and return it.

Parameters:
  • port_name – The port on which a message is to be received.

  • slot – The slot to receive the message on, if any.

Returns:

The received message, with message.settings holding the settings overlay. The settings attribute is guaranteed to not be None.

Raises:

RuntimeError – If the network connection had an error, or the message number was incorrect.

restore_state(state: CommunicatorState) → None[source]

Restore a previously saved state.

Parameters:

state – The state to restore, as returned by get_state().

send_message(port_name: str, message: Message, slot: int | None = None) → None[source]

Send a message and settings to the outside world.

Sending is non-blocking, a copy of the message will be made and stored until the receiver is ready to receive it.

Parameters:
  • port_name – The port on which this message is to be sent.

  • message – The message to be sent.

  • slot – The slot to send the message on, if any.

set_peer_info(peer_info: PeerInfo) → None[source]

Inform this Communicator about its peers.

This tells the Communicator about its peers, so that it can route messages accordingly. The TimelineManager is also created here, since it needs the port manager’s ports to already be connected to their peers.

Parameters:

peer_info – Information about the peers.

set_receive_timeout(receive_timeout: float) → None[source]

Update the timeout after which the manager is notified that we are waiting for a message.

Parameters:

receive_timeout – Timeout (seconds). A negative number disables the deadlock notification mechanism.

shutdown() → None[source]

Shuts down the Communicator, closing connections.

class libmuscle.communicator.Message(timestamp: float, next_timestamp: float | None = None, data: Any = None, settings: Settings | None = None)[source]

Bases: object

A message to be sent or received.

This class describes a message to be sent or that has been received.

timestamp

Simulation time for which this data is valid.

Type:

float

next_timestamp

Simulation time for the next message to be transmitted through this port.

Type:

Optional[float]

data

An object to send or that was received.

Type:

MessageObject

settings

Overlay settings to send or that was received.

Type:

Settings

exception libmuscle.communicator.PortClosed[source]

Bases: Exception

Exception raised when a port was closed during the F_INIT prereceive