GitHub

API reference for worker.

The initial message of RPC workers (Effect's RpcWorker).

A worker client (rpc.client.layer_protocol_worker) can send each worker one value before any request: provide InitialMessage with layer_initial_message(schema, build), and read it in the worker with initial_message(schema), which waits for it.

Re-exports

Types

TypeDefinitionDescription
Encodedtype Encoded = tuple[object, tuple[object, ...]]

An encoded initial message and the values to transfer with it.

Classes

Functions

make_initial_message

make_initial_message[A, E = Any, R = Any](schema, eff) -> Effect[Encoded, E | S.SchemaError, R]

Run eff and encode its value with schema's JSON codec, collecting its transferables.

ParameterTypeDescription
schemaS.Codec[A, Any, Any, Any]
effEffect[A, E, R]

Returns Effect[Encoded, E | S.SchemaError, R]

def make_initial_message[A, E = Any, R = Any](
    schema: S.Codec[A, Any, Any, Any], eff: Effect[A, E, R]
) -> Effect[Encoded, E | S.SchemaError, R]:
    """Run ``eff`` and encode its value with ``schema``'s JSON codec,
    collecting its transferables."""
    require_effect(eff, "eff")
    encode = S.encode_effect(S.to_codec_json(schema))

    def encoded(value: A) -> Effect[Encoded, S.SchemaError, Any]:
        collector = make_collector_unsafe()
        return (
            encode(value)
            .pipe(provide_service(Collector, collector))
            .map(lambda data: (data, tuple(collector.clear_unsafe())))
        )

    return eff.flat_map(encoded)

layer_initial_message

layer_initial_message[A, R = Any](schema, build) -> Layer[InitialMessage, Any, R]

Provide InitialMessage from build, run with the layer's services each time a worker spawns; an encoding failure is a defect.

ParameterTypeDescription
schemaS.Codec[A, Any, Any, Any]
buildEffect[A, Any, R]

Returns Layer[InitialMessage, Any, R]

def layer_initial_message[A, R = Any](
    schema: S.Codec[A, Any, Any, Any], build: Effect[A, Any, R]
) -> Layer[InitialMessage, Any, R]:
    """Provide ``InitialMessage`` from ``build``, run with the layer's
    services each time a worker spawns; an encoding failure is a defect."""
    require_effect(build, "build")

    def made(services: Context[Any]) -> Effect[Encoded]:
        return make_initial_message(schema, build).or_die().pipe(provide_context(services))

    return layer_effect(InitialMessage, context().map(made))

initial_message

initial_message[A](schema) -> Effect[A, NoSuchElementException | S.SchemaError, ServerProtocol]

The initial message the client sent this worker, decoded with schema's JSON codec; fails when the protocol has none.

ParameterTypeDescription
schemaS.Codec[A, Any, Any, Any]

Returns Effect[A, NoSuchElementException | S.SchemaError, ServerProtocol]

def initial_message[A](
    schema: S.Codec[A, Any, Any, Any],
) -> Effect[A, NoSuchElementException | S.SchemaError, ServerProtocol]:
    """The initial message the client sent this worker, decoded with
    ``schema``'s JSON codec; fails when the protocol has none."""
    decode = S.decode_unknown_effect(S.to_codec_json(schema))
    return (
        ServerProtocol()
        .flat_map(lambda protocol: protocol.initial_message)
        .flat_map(_from_option)
        .flat_map(decode)
    )