Define sync hass.create_task function (#50788)

pull/50799/head
Ruslan Sayfutdinov 2021-05-17 20:54:06 +01:00 committed by GitHub
parent 1e10772497
commit 5ad71b5e45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -373,6 +373,13 @@ class HomeAssistant:
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
def async_create_task(self, target: Coroutine) -> asyncio.tasks.Task:
"""Create a task from within the eventloop.

View File

@ -233,6 +233,29 @@ async def test_async_add_job_pending_tasks_coro(hass):
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):
"""Run an executor in pending tasks."""
call_count = []