Skip to content

Echo¤

Example of an Echo Service.

The Echo Service returns the arguments and keyword arguments that were sent from a Client in a request.

Start the Broker¤

Start the Broker by running the following command.

msl-network start

Start the Echo Service¤

Open another terminal and start the Echo Service by running the following command.

python -c "from msl.examples.network import run_echo; run_echo()"
Expand to show the source code of the Echo Service

"""Example echo Service.

Returns the arguments and keyword arguments that were sent from a Client.
"""

from __future__ import annotations

from typing import Any

from msl.network import Service


class Echo(Service):
    """Example Service that echos the arguments of the request."""

    @staticmethod
    def echo(*args: Any, **kwargs: Any) -> tuple[tuple[Any, ...], dict[str, Any]]:
        """Returns the arguments and keyword arguments sent from a Client."""
        return args, kwargs


if __name__ == "__main__":
    echo = Echo()

    # Connect the Service to the Broker (runs the event loop "forever")
    echo.connect()

Run the Client¤

Connect to the Broker as a Client, link with the Echo Service then send requests.

from msl.network import Client

with Client() as client:
    link = client.link("Echo")
    print(link.echo())
    print(link.echo(1, "hi", x=9.1))