from functools import wraps
import threading
import psutil
from time import time, sleep
class MetricsThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.max_memory = 0
self.max_cpu = 0
self.killed = False
def run(self):
while True:
if self.killed:
break
p = psutil.Process()
memory_info = p.memory_full_info()
memory = memory_info.rss / 1024 / 1024
cpu = p.cpu_percent(1)
if memory > self.max_memory:
self.max_memory = memory
# print("max_memory:{}".format(self.max_memory))
if cpu > self.max_cpu:
self.max_cpu = cpu
# print("max_cpu:{}".format(self.max_cpu))
sleep(0.1)
class MonitorFunc(Decorator):
start_time = None
end_time = None
res = False
closed = False
@classmethod
def memory_cpu(cls, func_name=""):
"""
监控函数运行内存和CPU
:res: res 默认异步函数
:return: 注解函数
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
thread = MetricsThread()
thread.setDaemon(True)
thread.start()
cls.closed = kwargs.get('closed')
cls.start_time = time()
result = func(*args, **kwargs)
cls.end_time = time()
max_memory = thread.max_memory
max_cpu = thread.max_cpu
thread.killed = True
print(
'\n[MEMORY AND CPU] {} use max_memory:{}, max_cpu:{} \n'.format(func_name, max_memory, max_cpu))
return result
return wrapper
return decorator
@classmethod
def exception(cls):
"""
异常处理的装饰器
:return: func的返回
"""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
if len(args) > 0:
sf = args[0]
if isinstance(sf, object):
if hasattr(sf, '_error_cnt'):
res = {}
try:
res = func(*args, **kwargs)
except Exception as e:
print(e, f'handle.{func.__name__} run error!!')
setattr(sf, '_error_cnt', 1)
res = sf
finally:
return res
return wrapper
return decorator
python监控函数运行占用最大内存和CPU最大使用率
最新推荐文章于 2024-04-23 10:53:33 发布