Improve performance of verify_event_loop_thread (#118198)

This commit is contained in:
J. Nick Koston 2024-05-26 17:39:15 -10:00 committed by GitHub
parent 87fc27eeae
commit 25f199c39c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 3 deletions

View file

@ -434,12 +434,13 @@ class HomeAssistant:
self.import_executor = InterruptibleThreadPoolExecutor( self.import_executor = InterruptibleThreadPoolExecutor(
max_workers=1, thread_name_prefix="ImportExecutor" 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: def verify_event_loop_thread(self, what: str) -> None:
"""Report and raise if we are not running in the event loop thread.""" """Report and raise if we are not running in the event loop thread."""
if ( if self._loop_thread_id != threading.get_ident():
loop_thread_ident := self.loop.__dict__.get("_thread_ident")
) and loop_thread_ident != threading.get_ident():
from .helpers import frame # pylint: disable=import-outside-toplevel from .helpers import frame # pylint: disable=import-outside-toplevel
# frame is a circular import, so we import it here # frame is a circular import, so we import it here

View file

@ -174,6 +174,7 @@ def get_test_home_assistant() -> Generator[HomeAssistant, None, None]:
"""Run event loop.""" """Run event loop."""
loop._thread_ident = threading.get_ident() loop._thread_ident = threading.get_ident()
hass._loop_thread_id = loop._thread_ident
loop.run_forever() loop.run_forever()
loop_stop_event.set() loop_stop_event.set()