Env Server

EnvServer owns a Python environment object and exposes it as an RLMesh environment endpoint. The served endpoint supports both local loopback use and process boundaries where a model or evaluator connects through RemoteEnv or RemoteVectorEnv.

Environment Contract

The server inspects the environment once during construction and caches an EnvContract. The contract describes the endpoint id, spaces, render mode, metadata, and number of environments. EnvServer.spec is an alias for EnvServer.env_contract so code that expects a Gymnasium-style spec field can still reach the same RLMesh contract.

server = rlmesh.EnvServer(env, "127.0.0.1:5555")
print(server.env_contract.id)
print(server.env_contract.observation_space.kind)
print(server.spec.action_space.kind)

See Contracts and Specs for contract fields.

Bind Address Environment Variables

When RLMesh serves an environment through its bootstrap entrypoint (for example inside a sandbox container), the bind address follows a single canonical contract so that hosts and downstream images agree on where the environment listens:

Variable

Meaning

RLMESH_ADDRESS

Full bind address (host:port, port, tcp://host:port, unix:///...). When set, it wins.

RLMESH_PORT

Port-only fallback, bound on 0.0.0.0, used when RLMESH_ADDRESS is unset (default 50051).

RLMESH_ENV_ADDRESS

Deprecated alias for RLMESH_ADDRESS; read only when it is unset.

RLMESH_ENV_PORT

Deprecated alias for RLMESH_PORT; read only when both addresses are unset.

RLMESH_ADDRESS is the preferred knob; it accepts the same forms as the EnvServer address argument, so a non-default host or a Unix socket can be selected without code changes. RLMESH_ENV_ADDRESS / RLMESH_ENV_PORT are deprecated aliases kept for backward compatibility. Constructing EnvServer directly in your own process ignores these variables. Pass address/host/port explicitly.

API

class rlmesh.EnvServer[source]

Bases: object

Serves an RLMesh-compatible environment.

Parameters:
  • env – Environment satisfying the RLMesh protocols.

  • address – Optional bind address. Supports "tcp://host:port", "host:port", "port", and "unix:///path/to/socket.sock". Defaults to "tcp://127.0.0.1:0" when omitted.

  • host – TCP host helper used when address is omitted.

  • port – TCP port helper used when address is omitted.

  • path – Unix socket path helper used when address is omitted.

  • transport – Explicit transport selector.

  • options – Optional serve lifecycle options controlling remote shutdown, idle shutdown, drain timeout, and close timeout.

  • tags – Optional adapter env tags (rlmesh.adapters.EnvTags) to publish for this env. They are validated against the env’s spaces and merged into its metadata, so a model client can resolve an adapter from the contract alone (see rlmesh.adapters.resolve_from_contract()).

Examples

>>> from rlmesh import EnvServer, spaces
>>>
>>> class TinyEnv:
...     observation_space = spaces.from_gymnasium_space(
...         __import__("gymnasium").spaces.Discrete(4)
...     )
...     action_space = spaces.from_gymnasium_space(
...         __import__("gymnasium").spaces.Discrete(2)
...     )
...
...     def reset(self, *, seed=None, options=None):
...         return 0, {}
...
...     def step(self, action):
...         return 0, 0.0, False, False, {}
...
...     def close(self):
...         return None
>>> server = EnvServer(TinyEnv(), "localhost:5555")
>>> server.serve()
__init__(env, address=None, *, host=None, port=None, path=None, transport=None, options=None, tags=None)[source]
Parameters:
  • env (EnvLike)

  • address (str | None)

  • host (str | None)

  • port (int | None)

  • path (str | None)

  • transport (Transport | None)

  • options (ServeOptions | None)

  • tags (EnvTags | None)

Return type:

None

property address: str[source]

Get the bound server address.

property env_contract: EnvContract[source]

Environment contract served by this endpoint.

property spec: EnvContract[source]

Alias for env_contract.

serve()[source]

Start serving the environment (blocking).

Return type:

None

start()[source]

Start serving the environment on a background thread.

Return type:

None

wait(timeout=None)[source]

Wait for a background server to stop.

Parameters:

timeout (float | None) – Optional timeout in seconds. None waits indefinitely.

Returns:

True if the server has stopped, or False if the timeout elapsed.

Return type:

bool

shutdown()[source]

Stop the server if it is running.

Return type:

None