python监控函数运行占用最大内存和CPU最大使用率

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值