mirror of
https://github.com/esphome/aioesphomeapi.git
synced 2024-12-26 17:27:40 +01:00
Reduce latancy when sending complex messages (#473)
This commit is contained in:
parent
ce07e11e93
commit
0a0172fa89
@ -612,6 +612,12 @@ class APIConnection:
|
|||||||
|
|
||||||
:raises TimeoutAPIError: if a timeout occured
|
:raises TimeoutAPIError: if a timeout occured
|
||||||
"""
|
"""
|
||||||
|
# Send the message right away to reduce latency.
|
||||||
|
# This is safe because we are not awaiting between
|
||||||
|
# sending the message and registering the handler
|
||||||
|
|
||||||
|
self.send_message(send_msg)
|
||||||
|
# Unsafe to await between sending the message and registering the handler
|
||||||
fut: asyncio.Future[None] = self._loop.create_future()
|
fut: asyncio.Future[None] = self._loop.create_future()
|
||||||
responses = []
|
responses = []
|
||||||
|
|
||||||
@ -627,12 +633,13 @@ class APIConnection:
|
|||||||
self._message_handlers.setdefault(msg_type, []).append(on_message)
|
self._message_handlers.setdefault(msg_type, []).append(on_message)
|
||||||
|
|
||||||
self._read_exception_futures.add(fut)
|
self._read_exception_futures.add(fut)
|
||||||
|
# Now safe to await since we have registered the handler
|
||||||
|
|
||||||
# We must not await without a finally or
|
# We must not await without a finally or
|
||||||
# the message could fail to be removed if the
|
# the message could fail to be removed if the
|
||||||
# the await is cancelled
|
# the await is cancelled
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.send_message(send_msg)
|
|
||||||
async with async_timeout.timeout(timeout):
|
async with async_timeout.timeout(timeout):
|
||||||
await fut
|
await fut
|
||||||
except asyncio.TimeoutError as err:
|
except asyncio.TimeoutError as err:
|
||||||
|
Loading…
Reference in New Issue
Block a user