Decrease event loop latency by binding time.monotonic to loop.time directly (#98288)

* Decrease event loop latency by binding time.monotonic to loop.time directly

This is a small improvment to decrease event loop latency. While the goal is
is to reduce Bluetooth connection time latency, everything using asyncio
is a bit more responsive as a result.

* relo per comments

* fix too fast by adding resolution, ensure monotonic time is patchable by freezegun

* fix test that freezes time too late and has a race loop
This commit is contained in:
J. Nick Koston
2023-08-13 19:37:45 -05:00
committed by GitHub
parent 07e20e1eab
commit 790c1bc251
4 changed files with 22 additions and 4 deletions

View File

@@ -2,8 +2,9 @@
from __future__ import annotations
import datetime
import time
from homeassistant import util
from homeassistant import runner, util
from homeassistant.util import dt as dt_util
@@ -12,5 +13,11 @@ def _utcnow() -> datetime.datetime:
return datetime.datetime.now(datetime.UTC)
def _monotonic() -> float:
"""Make monotonic patchable by freezegun."""
return time.monotonic()
dt_util.utcnow = _utcnow # type: ignore[assignment]
util.utcnow = _utcnow # type: ignore[assignment]
runner.monotonic = _monotonic # type: ignore[assignment]