-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for importing integrations in the executor #111336
Conversation
alternative solution to #111331
Hey there @home-assistant/matter, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
|
|
2024-02-25 02:52:40.237 DEBUG (MainThread) [homeassistant.config_entries] Loaded flow for denonavr in 4.57s |
cut start up time in half on the green! bluetooth went from ~12s to 0.6s because its not longer blocked |
This is pretty awesome 😎 as I thought this wasn't able to be improved for years. It took the |
It also solves all the storage loading getting backed up as well |
|
Manually updated a yellow. There are a few more that block the loop for followups
full log:
|
Manually updated a blue There are a few more that block the loop for followups
Full log:
|
Going to test on a Raspberry Pi 3 (32-bit) next. That one takes a while for everything so it will be a good test |
rpi 3b 32bit There are a few more that block the loop for followups. I'm on the fence on these since rpi 3bs are less and less common though:
full log
|
@bdraco just for better understanding, were/are there IO's caused by this imports which block the event loop or were this just pure compute/CPU bound? I guess the latter makes this solution essentially Home Assistant startup "multi-threaded", hence better leverage multicore CPUs such as Greens quad-core CPU. |
Thinking a bit more about it, I guess Python on a lower layer always has to do some IO to import modules in the end. 🤔 In any case, even if there is no IO involved, making them run in an executor makes this leverage multi-threading. I was wondering if Python is handling multi-threaded imports well (as in, a) is it safe and b) does it support truly multi-threaded or is there a single global lock which serializes import). It seems it is safe and at least since Python 3.3 it does lock per-module, so this makes it truly parallel 🎉 (according to this StackOverflow post). |
For some modules with large files (both size or count), its I/O bound. For |
I need to update the docs/blog post since its only |
merge is to fix conflicts |
was not added to the original PR to contain scope identified at: #111336 (comment)
was not added to the original PR to contain scope identified at: #111336 (comment)
was not added to the original PR to contain scope identified at: #111336 (comment)
…#111519) was not added to the original PR to contain scope identified at: #111336 (comment)
…1520) was not added to the original PR to contain scope identified at: #111336 (comment)
Proposed change
Load integrations in the executor if
import_executor
is specified inmanifest.json
to avoid the code import blocking the event loop.While investigating Bluetooth problems on a Home Assistant Green, I found that Bluetooth failed to start because the event loop was blocked for 7-12s from loading
matter
which caused the system to be unstable and timeout many taskshome-assistant-libs/python-matter-server#578. The problem was not Bluetooth itself, as once the source of the event loop blocking was addressed and Bluetooth is no longer blocked, the startup time is < 1s.
I expect this change will fix hundreds of unexplained startup issues. It will also solve some instability at run time since the same problem happens when an integration is discovered and the config flow is loaded since it loads the modules there as well.
This is opt-in because some modules are cheap to import or call
asyncio.get_running_loop()
or similar during import so they can't be imported in the executor.Type of change
Additional information
MatterClient
blocks the event loop for ~7-12 seconds. home-assistant-libs/python-matter-server#578Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: