From 677b06f502793d12ff4d0a9f25b786ab902e8440 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Sat, 27 Jan 2024 13:05:31 +0100 Subject: [PATCH] Add comment to explain not using the core API in MQTT client (#108942) --- homeassistant/components/mqtt/client.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/homeassistant/components/mqtt/client.py b/homeassistant/components/mqtt/client.py index 14a18354b01..164632cdd10 100644 --- a/homeassistant/components/mqtt/client.py +++ b/homeassistant/components/mqtt/client.py @@ -216,6 +216,10 @@ def subscribe( def remove() -> None: """Remove listener convert.""" + # MQTT messages tend to be high volume, + # and since they come in via a thread and need to be processed in the event loop, + # we want to avoid hass.add_job since most of the time is spent calling + # inspect to figure out how to run the callback. hass.loop.call_soon_threadsafe(async_remove) return remove @@ -796,6 +800,10 @@ class MQTT: self, _mqttc: mqtt.Client, _userdata: None, msg: mqtt.MQTTMessage ) -> None: """Message received callback.""" + # MQTT messages tend to be high volume, + # and since they come in via a thread and need to be processed in the event loop, + # we want to avoid hass.add_job since most of the time is spent calling + # inspect to figure out how to run the callback. self.loop.call_soon_threadsafe(self._mqtt_handle_message, msg) @lru_cache(None) # pylint: disable=method-cache-max-size-none