Skip to content

Commit

Permalink
Add comments why we use loop.create_task in core (home-assistant#112372)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinHjelmare authored Mar 5, 2024
1 parent 017b262 commit 4d82ea5
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,8 @@ def async_add_hass_job(
hassjob.target = cast(
Callable[..., Coroutine[Any, Any, _R]], hassjob.target
)
# Use loop.create_task
# to avoid the extra function call in asyncio.create_task.
task = self.loop.create_task(hassjob.target(*args), name=hassjob.name)
elif hassjob.job_type is HassJobType.Callback:
if TYPE_CHECKING:
Expand Down Expand Up @@ -649,6 +651,8 @@ def async_create_task(
if task.done():
return task
else:
# Use loop.create_task
# to avoid the extra function call in asyncio.create_task.
task = self.loop.create_task(target, name=name)
self._tasks.add(task)
task.add_done_callback(self._tasks.remove)
Expand All @@ -671,6 +675,8 @@ def async_create_background_task(
if task.done():
return task
else:
# Use loop.create_task
# to avoid the extra function call in asyncio.create_task.
task = self.loop.create_task(target, name=name)
self._background_tasks.add(task)
task.add_done_callback(self._background_tasks.remove)
Expand Down

0 comments on commit 4d82ea5

Please sign in to comment.