Skip to content

grpc

GrpcLearner(backend)

Bases: SupervisedLearner, Model

Binding for an external learner using gRPC.

This learner class connects to an external learner using gRPC. Processed data for training or inference is sent to the learner and results are received from it.

Create a GrpcLearner.

Parameters:

Name Type Description Default
backend _Backend

The backend to use for the learner.

required
Source code in src/flowcean/learners/grpc/learner.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def __init__(
    self,
    backend: _Backend,
) -> None:
    """Create a GrpcLearner.

    Args:
        backend: The backend to use for the learner.
    """
    super().__init__()
    self._backend = backend
    self.channel = grpc.insecure_channel(
        self._backend.server_address,
        options=[
            (
                "grpc.max_send_message_length",
                MAX_MESSAGE_LENGTH,
            ),
            (
                "grpc.max_receive_message_length",
                MAX_MESSAGE_LENGTH,
            ),
        ],
    )
    logger.info("Establishing gRPC connection...")
    self._stub = LearnerStub(self.channel)

with_address(address) classmethod

Create a GrpcLearner with a specific address.

Parameters:

Name Type Description Default
address str

The address of the learner.

required

Returns:

Type Description
Self

A GrpcLearner instance.

Source code in src/flowcean/learners/grpc/learner.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
@classmethod
def with_address(
    cls,
    address: str,
) -> Self:
    """Create a GrpcLearner with a specific address.

    Args:
        address: The address of the learner.

    Returns:
        A GrpcLearner instance.
    """
    backend = _AddressBackend(address)
    return cls(backend=backend)

run_docker(image, internal_port=8080, *, pull=True) classmethod

Create a GrpcLearner running in a Docker container.

Parameters:

Name Type Description Default
image str

The name of the Docker image to run.

required
internal_port int

port the learner listens on inside the container.

8080
pull bool

Whether to pull the image from the registry.

True

Returns:

Type Description
Self

A GrpcLearner instance.

Source code in src/flowcean/learners/grpc/learner.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
@classmethod
def run_docker(
    cls,
    image: str,
    internal_port: int = 8080,
    *,
    pull: bool = True,
) -> Self:
    """Create a GrpcLearner running in a Docker container.

    Args:
        image: The name of the Docker image to run.
        internal_port: port the learner listens on inside the container.
        pull: Whether to pull the image from the registry.

    Returns:
        A GrpcLearner instance.
    """
    backend = _DockerBackend(image, internal_port, pull=pull)
    return cls(backend=backend)

__del__()

Close the gRPC channel.

Source code in src/flowcean/learners/grpc/learner.py
206
207
208
def __del__(self) -> None:
    """Close the gRPC channel."""
    self.channel.close()