From 25f199c39c42c7200fd3a0b47090939522d4a4bb Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 26 May 2024 17:39:15 -1000 Subject: [PATCH] Improve performance of verify_event_loop_thread (#118198) --- homeassistant/core.py | 7 ++++--- tests/common.py | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index 72e33a1d786..6c2d7711a0d 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -434,12 +434,13 @@ class HomeAssistant: self.import_executor = InterruptibleThreadPoolExecutor( max_workers=1, thread_name_prefix="ImportExecutor" ) + self._loop_thread_id = getattr( + self.loop, "_thread_ident", getattr(self.loop, "_thread_id") + ) def verify_event_loop_thread(self, what: str) -> None: """Report and raise if we are not running in the event loop thread.""" - if ( - loop_thread_ident := self.loop.__dict__.get("_thread_ident") - ) and loop_thread_ident != threading.get_ident(): + if self._loop_thread_id != threading.get_ident(): from .helpers import frame # pylint: disable=import-outside-toplevel # frame is a circular import, so we import it here diff --git a/tests/common.py b/tests/common.py index 252e5309411..6e7cf1b21f3 100644 --- a/tests/common.py +++ b/tests/common.py @@ -174,6 +174,7 @@ def get_test_home_assistant() -> Generator[HomeAssistant, None, None]: """Run event loop.""" loop._thread_ident = threading.get_ident() + hass._loop_thread_id = loop._thread_ident loop.run_forever() loop_stop_event.set()