flask views视图函数封装参考

import logging
import psutil
from flask import Flask
from flask import request

app = Flask(__name__)


def getMemCpu():  # 系统信息
    data = psutil.virtual_memory()
    memory = "Memory :%d" % (int(round(data.percent))) + "%" + " "
    cpu = "CPU:%0.2f" % psutil.cpu_percent(interval=1) + "%"
    return memory + cpu


def mAPI(func):  # di
    if request.method == "POST":
        try:
            params = request.json
            if params == {}:
                data_ = {}
            else:
                data_ = params

            res = func()  # 无参数的情况
            # res = func(json.dumps(data_))  # 有参数的情况
            logging.info(f'res:{res}')

            return res
        except Exception as e:
            return "unExpect"


@app.route('/algo/V1_0', methods=['POST'])
def invokeAlgorithmtianqing_getPassengersBaseBusLine():

    return mAPI(getMemCpu)

 

posted @ 2023-07-25 11:37  ddzhen  阅读(19)  评论(0编辑  收藏  举报