async_utils.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # Adapted from [aiodebug](https://gitlab.com/quantlane/libs/aiodebug)
  2. # Copyright 2016-2022 Quantlane s.r.o.
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. # Modifications:
  13. # - Removed the dependency to `logwood`.
  14. # - Renamed `monitor_loop_lag.enable()` to just `enable_monitor_loop_lag()`.
  15. # - Miscellaneous changes to make it work with Ray.
  16. import asyncio
  17. import asyncio.events
  18. from typing import Callable, Optional
  19. def enable_monitor_loop_lag(
  20. callback: Callable[[float], None],
  21. interval_s: float = 0.25,
  22. loop: Optional[asyncio.AbstractEventLoop] = None,
  23. ) -> None:
  24. """
  25. Start logging event loop lags to the callback. In ideal circumstances they should be
  26. very close to zero. Lags may increase if event loop callbacks block for too long.
  27. Note: this works for all event loops, including uvloop.
  28. :param callback: Callback to call with the lag in seconds.
  29. """
  30. if loop is None:
  31. loop = asyncio.get_running_loop()
  32. if loop is None:
  33. raise ValueError("No provided loop, nor running loop found.")
  34. async def monitor():
  35. while loop.is_running():
  36. t0 = loop.time()
  37. await asyncio.sleep(interval_s)
  38. lag = loop.time() - t0 - interval_s # Should be close to zero.
  39. callback(lag)
  40. loop.create_task(monitor(), name="async_utils.monitor_loop_lag")