Limit concurrency of async_get_integration to avoid creating extra threads (#43085)

* Limit concurrency of async_get_integration to avoid creating extra threads

Since async_get_integration is waiting on the disk most of the time
it would end up creating many new threads because the disk could
not deliver the data in time.

* pylint
This commit is contained in:
J. Nick Koston 2020-11-10 21:34:54 -10:00 committed by GitHub
parent 518e462e9a
commit 94bf55e29b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 68 additions and 9 deletions

View file

@ -1,4 +1,7 @@
"""Tests for async util methods from Python source."""
import asyncio
import time
import pytest
from homeassistant.util import async_ as hasync
@ -144,3 +147,25 @@ def test_protect_loop_sync():
hasync.protect_loop(calls.append)(1)
assert len(mock_loop.mock_calls) == 1
assert calls == [1]
async def test_gather_with_concurrency():
"""Test gather_with_concurrency limits the number of running tasks."""
runs = 0
now_time = time.time()
async def _increment_runs_if_in_time():
if time.time() - now_time > 0.1:
return -1
nonlocal runs
runs += 1
await asyncio.sleep(0.1)
return runs
results = await hasync.gather_with_concurrency(
2, *[_increment_runs_if_in_time() for i in range(4)]
)
assert results == [2, 2, -1, -1]