mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Set executor to 15 and help to reduce flooting async core with updates (#4252)
* Set executor to 15 and help to reduce flooting async core with udpates * fix typing * if it a executor, wait * address comments from paulus * add space for style :) * fix spell * Update entity_component.py * Update entity_component.py
This commit is contained in:
parent
880ef8af48
commit
618a86a37c
@ -57,7 +57,7 @@ SERVICE_CALL_LIMIT = 10 # seconds
|
|||||||
ENTITY_ID_PATTERN = re.compile(r"^(\w+)\.(\w+)$")
|
ENTITY_ID_PATTERN = re.compile(r"^(\w+)\.(\w+)$")
|
||||||
|
|
||||||
# Size of a executor pool
|
# Size of a executor pool
|
||||||
EXECUTOR_POOL_SIZE = 10
|
EXECUTOR_POOL_SIZE = 15
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -289,6 +289,7 @@ class EntityPlatform(object):
|
|||||||
self.entity_namespace = entity_namespace
|
self.entity_namespace = entity_namespace
|
||||||
self.platform_entities = []
|
self.platform_entities = []
|
||||||
self._async_unsub_polling = None
|
self._async_unsub_polling = None
|
||||||
|
self._process_updates = False
|
||||||
|
|
||||||
def add_entities(self, new_entities, update_before_add=False):
|
def add_entities(self, new_entities, update_before_add=False):
|
||||||
"""Add entities for a single platform."""
|
"""Add entities for a single platform."""
|
||||||
@ -348,14 +349,37 @@ class EntityPlatform(object):
|
|||||||
self._async_unsub_polling()
|
self._async_unsub_polling()
|
||||||
self._async_unsub_polling = None
|
self._async_unsub_polling = None
|
||||||
|
|
||||||
@callback
|
@asyncio.coroutine
|
||||||
def _update_entity_states(self, now):
|
def _update_entity_states(self, now):
|
||||||
"""Update the states of all the polling entities.
|
"""Update the states of all the polling entities.
|
||||||
|
|
||||||
|
To protect from flooding the executor, we will update async entities
|
||||||
|
in parallel and other entities sequential.
|
||||||
|
|
||||||
This method must be run in the event loop.
|
This method must be run in the event loop.
|
||||||
"""
|
"""
|
||||||
for entity in self.platform_entities:
|
if self._process_updates:
|
||||||
if entity.should_poll:
|
return
|
||||||
self.component.hass.async_add_job(
|
self._process_updates = True
|
||||||
entity.async_update_ha_state(True)
|
|
||||||
)
|
try:
|
||||||
|
tasks = []
|
||||||
|
to_update = []
|
||||||
|
|
||||||
|
for entity in self.platform_entities:
|
||||||
|
if not entity.should_poll:
|
||||||
|
continue
|
||||||
|
|
||||||
|
update_coro = entity.async_update_ha_state(True)
|
||||||
|
if hasattr(entity, 'async_update'):
|
||||||
|
tasks.append(update_coro)
|
||||||
|
else:
|
||||||
|
to_update.append(update_coro)
|
||||||
|
|
||||||
|
for update_coro in to_update:
|
||||||
|
yield from update_coro
|
||||||
|
|
||||||
|
if tasks:
|
||||||
|
yield from asyncio.wait(tasks, loop=self.component.hass.loop)
|
||||||
|
finally:
|
||||||
|
self._process_updates = False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user