From 782a934af9a54ea3026ac075673ca40dc5359c3e Mon Sep 17 00:00:00 2001 From: timofey Date: Sat, 5 Nov 2022 07:14:23 +0000 Subject: [PATCH] block monitor --- v6d2ctx/pain.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/v6d2ctx/pain.py b/v6d2ctx/pain.py index 769c8ab..c14071a 100644 --- a/v6d2ctx/pain.py +++ b/v6d2ctx/pain.py @@ -1,3 +1,4 @@ +import asyncio import os import time @@ -48,3 +49,28 @@ class FrameTrace(Instrumentation): print(frame.f_code.co_filename, frame.f_lineno) frame = frame.f_back return await method(*args, **kwargs) + + +class ABlockMonitor: + __task: asyncio.Future + __max: float + + def __init__(self, threshold=0.0) -> None: + self.__max = threshold + + async def _monitor(self): + while True: + delta = 10 + t = time.time() + await asyncio.sleep(delta) + spent = time.time() - t + delay = spent - delta + if delay > self.__max: + self.__max = delay + print(f'block monitor reached new peak delay {delay:.3f}') + + async def __aenter__(self): + self.__task = asyncio.get_running_loop().create_task(self._monitor()) + + async def __aexit__(self, exc_type, exc_val, exc_tb): + self.__task.cancel()