Import stream in the executor to avoid blocking the event loop (#112078)

* Import stream in the executor to avoid blocking the event loop

This one has some large deps

* one more place

* avoid call if no change

* just in case
This commit is contained in:
J. Nick Koston 2024-03-02 17:00:28 -10:00 committed by GitHub
parent aaa2d8745f
commit dc3c7c95f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 12 deletions

View File

@ -188,22 +188,26 @@ CONFIG_SCHEMA = vol.Schema(
)
@callback
def update_pyav_logging(_event: Event | None = None) -> None:
"""Adjust libav logging to only log when the stream logger is at DEBUG."""
def set_pyav_logging(enable: bool) -> None:
"""Turn PyAV logging on or off."""
import av # pylint: disable=import-outside-toplevel
av.logging.set_level(av.logging.VERBOSE if enable else av.logging.FATAL)
# enable PyAV logging iff Stream logger is set to debug
set_pyav_logging(logging.getLogger(__name__).isEnabledFor(logging.DEBUG))
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up stream."""
debug_enabled = _LOGGER.isEnabledFor(logging.DEBUG)
@callback
def update_pyav_logging(_event: Event | None = None) -> None:
"""Adjust libav logging to only log when the stream logger is at DEBUG."""
nonlocal debug_enabled
if (new_debug_enabled := _LOGGER.isEnabledFor(logging.DEBUG)) == debug_enabled:
return
debug_enabled = new_debug_enabled
# enable PyAV logging iff Stream logger is set to debug
set_pyav_logging(new_debug_enabled)
# Only pass through PyAV log messages if stream logging is above DEBUG
cancel_logging_listener = hass.bus.async_listen(
@ -213,7 +217,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
# at logging.WARNING. Set those Logger levels to logging.ERROR
for logging_namespace in ("libav.mp4", "libav.swscaler"):
logging.getLogger(logging_namespace).setLevel(logging.ERROR)
update_pyav_logging()
# This will load av so we run it in the executor
await hass.async_add_import_executor_job(set_pyav_logging, debug_enabled)
# Keep import here so that we can import stream integration without installing reqs
# pylint: disable-next=import-outside-toplevel

View File

@ -4,6 +4,7 @@
"codeowners": ["@hunterjm", "@uvjustin", "@allenporter"],
"dependencies": ["http"],
"documentation": "https://www.home-assistant.io/integrations/stream",
"import_executor": true,
"integration_type": "system",
"iot_class": "local_push",
"quality_scale": "internal",