block monitor

This commit is contained in:
AF 2022-11-05 07:14:23 +00:00
parent 2f0ac33c02
commit 782a934af9

View File

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