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()