robustness: vision/depth render off the event loop (deepcopy snapshot), chat auto-reconnect + friendly errors, demo catches connection failures
This commit is contained in:
+14
-5
@@ -11,6 +11,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import copy
|
||||
import io
|
||||
import math
|
||||
from typing import Any
|
||||
@@ -206,11 +207,18 @@ class RoomBridge(Bridge):
|
||||
)
|
||||
async def vision() -> VisionOutput:
|
||||
tick = self._tick()
|
||||
img, _ = renderer.render(world)
|
||||
buf = io.BytesIO()
|
||||
img.save(buf, format="PNG")
|
||||
# Render off the event loop so frames never stall the bridge under
|
||||
# load; deepcopy keeps the snapshot consistent with this tool call.
|
||||
snapshot = copy.deepcopy(world)
|
||||
img, _ = await asyncio.to_thread(renderer.render, snapshot)
|
||||
|
||||
def _encode() -> str:
|
||||
buf = io.BytesIO()
|
||||
img.save(buf, format="PNG")
|
||||
return base64.b64encode(buf.getvalue()).decode("ascii")
|
||||
|
||||
return VisionOutput(
|
||||
png_b64=base64.b64encode(buf.getvalue()).decode("ascii"),
|
||||
png_b64=await asyncio.to_thread(_encode),
|
||||
width=VISION_WIDTH,
|
||||
height=VISION_HEIGHT,
|
||||
tick=tick,
|
||||
@@ -222,7 +230,8 @@ class RoomBridge(Bridge):
|
||||
)
|
||||
async def depth(max_depth: float = 10.0) -> DepthOutput:
|
||||
tick = self._tick()
|
||||
_, grid = renderer.render(world, max_depth=max_depth)
|
||||
snapshot = copy.deepcopy(world)
|
||||
_, grid = await asyncio.to_thread(renderer.render, snapshot, max_depth)
|
||||
return DepthOutput(
|
||||
width=DEPTH_WIDTH,
|
||||
height=DEPTH_HEIGHT,
|
||||
|
||||
Reference in New Issue
Block a user