Skip to content

sim2bot.Robot.wait_for_sim

wait_for_sim(timeout: Optional[float] = None, poll_interval: float = 0.5) -> list[RobotInfo]

Wait until a browser simulator connects and announces robots.

Parameters:

  • timeout (Optional[float], default: None ) –

    Maximum total wait in seconds, or None to wait forever.

  • poll_interval (float, default: 0.5 ) –

    Delay in seconds between unsuccessful discovery calls.

Returns:

Raises:

  • TimeoutError

    If no browser scene appears before timeout.

  • RuntimeError

    If the client is not connected.

  • ConnectionClosed

    If the WebSocket closes during discovery.

  • OSError

    If the selected transport cannot send a discovery request.

Examples:

This allows a controller to start before the browser::

with Robot(auto_bridge=True, wait_for_sim=True) as sim:
    robots = sim.describe()
Source code in sim2bot/client.py
def wait_for_sim(
    self,
    timeout: Optional[float] = None,
    poll_interval: float = 0.5,
) -> list[RobotInfo]:
    """Wait until a browser simulator connects and announces robots.

    Args:
        timeout: Maximum total wait in seconds, or ``None`` to wait forever.
        poll_interval: Delay in seconds between unsuccessful discovery calls.

    Returns:
        The non-empty list of discovered
        [`RobotInfo`][sim2bot.client.RobotInfo] objects.

    Raises:
        TimeoutError: If no browser scene appears before ``timeout``.
        RuntimeError: If the client is not connected.
        ConnectionClosed: If the WebSocket closes during discovery.
        OSError: If the selected transport cannot send a discovery request.

    Examples:
        This allows a controller to start before the browser::

            with Robot(auto_bridge=True, wait_for_sim=True) as sim:
                robots = sim.describe()
    """
    deadline = None if timeout is None else time.time() + timeout
    while True:
        describe_timeout = 1.0
        if deadline is not None:
            remaining = deadline - time.time()
            if remaining <= 0:
                raise TimeoutError("Timed out waiting for Sim2Bot browser simulator")
            describe_timeout = max(0.05, min(describe_timeout, remaining))

        robots = self.describe(timeout=describe_timeout)
        if robots:
            return robots

        if deadline is not None:
            remaining = deadline - time.time()
            if remaining <= 0:
                raise TimeoutError("Timed out waiting for Sim2Bot browser simulator")
            time.sleep(min(poll_interval, remaining))
        else:
            time.sleep(poll_interval)

Practical examples

Examples use objects discovered from the current scene rather than hard-coded robot metadata.

Wait indefinitely

with Robot(auto_bridge=True) as sim:
    robots = sim.wait_for_sim()
    print(robots[0].name)

Use a bounded startup timeout

with Robot(auto_bridge=True) as sim:
    try:
        robots = sim.wait_for_sim(timeout=20, poll_interval=0.25)
    except TimeoutError:
        print("Open Sim2Bot and connect its Bridge panel")

Guidance

Usage tip

A bounded timeout is better for services and CI; an infinite wait is convenient for interactive scripts.


See also