Add support for eager tasks (#111425)

* Add support for eager tasks

python 3.12 supports eager tasks

reading:
https://docs.python.org/3/library/asyncio-task.html#eager-task-factory
https://github.com/python/cpython/issues/97696

There are lots of places were we are unlikely to suspend, but we might
suspend so creating a task makes sense

* reduce

* revert entity

* revert

* coverage

* coverage

* coverage

* coverage

* fix test
This commit is contained in:
J. Nick Koston 2024-02-26 06:36:46 -10:00 committed by GitHub
parent 93cc6e0f36
commit 67e356904b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 162 additions and 18 deletions

View file

@ -915,6 +915,7 @@ class ConfigEntry:
hass: HomeAssistant,
target: Coroutine[Any, Any, _R],
name: str | None = None,
eager_start: bool = False,
) -> asyncio.Task[_R]:
"""Create a task from within the event loop.
@ -923,7 +924,7 @@ class ConfigEntry:
target: target to call.
"""
task = hass.async_create_task(
target, f"{name} {self.title} {self.domain} {self.entry_id}"
target, f"{name} {self.title} {self.domain} {self.entry_id}", eager_start
)
self._tasks.add(task)
task.add_done_callback(self._tasks.remove)
@ -932,7 +933,11 @@ class ConfigEntry:
@callback
def async_create_background_task(
self, hass: HomeAssistant, target: Coroutine[Any, Any, _R], name: str
self,
hass: HomeAssistant,
target: Coroutine[Any, Any, _R],
name: str,
eager_start: bool = False,
) -> asyncio.Task[_R]:
"""Create a background task tied to the config entry lifecycle.
@ -940,7 +945,7 @@ class ConfigEntry:
target: target to call.
"""
task = hass.async_create_background_task(target, name)
task = hass.async_create_background_task(target, name, eager_start)
self._background_tasks.add(task)
task.add_done_callback(self._background_tasks.remove)
return task