mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Define sync hass.create_task function (#50788)
This commit is contained in:
parent
1e10772497
commit
5ad71b5e45
@ -373,6 +373,13 @@ class HomeAssistant:
|
|||||||
|
|
||||||
return task
|
return task
|
||||||
|
|
||||||
|
def create_task(self, target: Coroutine) -> None:
|
||||||
|
"""Add task to the executor pool.
|
||||||
|
|
||||||
|
target: target to call.
|
||||||
|
"""
|
||||||
|
self.loop.call_soon_threadsafe(self.async_create_task, target)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_create_task(self, target: Coroutine) -> asyncio.tasks.Task:
|
def async_create_task(self, target: Coroutine) -> asyncio.tasks.Task:
|
||||||
"""Create a task from within the eventloop.
|
"""Create a task from within the eventloop.
|
||||||
|
@ -233,6 +233,29 @@ async def test_async_add_job_pending_tasks_coro(hass):
|
|||||||
assert len(call_count) == 2
|
assert len(call_count) == 2
|
||||||
|
|
||||||
|
|
||||||
|
async def test_async_create_task_pending_tasks_coro(hass):
|
||||||
|
"""Add a coro to pending tasks."""
|
||||||
|
call_count = []
|
||||||
|
|
||||||
|
async def test_coro():
|
||||||
|
"""Test Coro."""
|
||||||
|
call_count.append("call")
|
||||||
|
|
||||||
|
for _ in range(2):
|
||||||
|
hass.create_task(test_coro())
|
||||||
|
|
||||||
|
async def wait_finish_callback():
|
||||||
|
"""Wait until all stuff is scheduled."""
|
||||||
|
await asyncio.sleep(0)
|
||||||
|
await asyncio.sleep(0)
|
||||||
|
|
||||||
|
await wait_finish_callback()
|
||||||
|
|
||||||
|
assert len(hass._pending_tasks) == 2
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(call_count) == 2
|
||||||
|
|
||||||
|
|
||||||
async def test_async_add_job_pending_tasks_executor(hass):
|
async def test_async_add_job_pending_tasks_executor(hass):
|
||||||
"""Run an executor in pending tasks."""
|
"""Run an executor in pending tasks."""
|
||||||
call_count = []
|
call_count = []
|
||||||
|
Loading…
x
Reference in New Issue
Block a user