Repository navigation
fix: offload map parsing to worker thread to prevent event loop blocking - #966
Conversation
There was a problem hiding this comment.
Copilot review overview
🔵 Needs a closer look
Concurrent refreshes can apply stale map data; serialization or generation checks are needed.
Review effort: Lite
Findings: None
What changed in this PR
This pull request offloads V1 map parsing and rendering to worker threads to reduce asyncio event-loop blocking.
Changes:
- Runs map conversion via
asyncio.to_thread. - Adds threaded cached-map parsing.
- Reuses unchanged cached map content.
| File | Description |
|---|---|
roborock/devices/traits/v1/map_content.py |
Offloads map conversion to a worker thread. |
roborock/devices/traits/v1/home.py |
Offloads cached parsing and avoids redundant reparsing. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
b213233 to
10bbbda
Compare
10bbbda to
b925c8d
Compare
6f4275a to
377d0ec
Compare
Offload CPU-intensive map parsing and rendering to worker threads via `asyncio.to_thread` to prevent blocking the asyncio event loop. - In `MapContentTrait.refresh()`, offload `self.converter.convert` to `asyncio.to_thread`. - In `HomeTrait.discover_home()`, move cached map parsing to a helper function `_parse_cached_home_map_content` and run it via `asyncio.to_thread`. - Avoid reparsing cached maps in `HomeTrait` when raw bytes are unchanged. Addresses home-assistant/core#182656
377d0ec to
e6b3701
Compare
|
We also could probably get significant improvements in parsing time - may be something we want to look into in the future! Good change nonetheless! |
Good idea! Maybe we can do pass on map parsing test coverage to make sure we feel good there and also throw an agent at it. |
Description
Offload CPU-intensive map parsing and rendering to worker threads via
asyncio.to_threadto prevent blocking the asyncio event loop.When devices are actively cleaning and returning map updates, parsing the raw map payload and rendering the map into PNG format runs synchronously on the asyncio event loop. In multi-device setups or frequent update cycles, this locks the event loop and leads to high latency spikes in downstream consumers like Home Assistant (see home-assistant/core#182656).
Changes
MapContentTrait.refresh(): offloadself.converter.convert(response)to a worker thread usingawait asyncio.to_thread(self.converter.convert, response).HomeTrait:_parse_cached_home_map_content(...)and run it viaawait asyncio.to_thread(...)once, rather than runningasyncio.to_threadper item in a loop._parse_cached_home_map_contentwhen the cached bytes match the previously parsedraw_api_response.Realted to home-assistant/core#182656