暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

python 内存系列(6)-line_profiler在Flask 框架里面里使用

小儿来一壶枸杞酒泡茶 2021-03-07
1061

在 之前的介绍中,python 内存系列(4)-line_profiler库 统计每行代码的执行次数和执行时间 可以精确分析每行代码的运行时间,那如何在我们的之前写的相关的Web 服务中进行集成使用呢?


其实也很简单,既然我们的line_profiler可以定义成一个装饰器,直接的把定义的号的装饰器装饰在我们的路由上就可以了!

之前定义好的装饰器形式:

from line_profiler import LineProfiler
from functools import wraps

# 查询接口中每行代码执行的时间
def func_line_time(f):
    @wraps(f)
    def decorator(*args, **kwargs):
        func_return = f(*args, **kwargs)
        profile = LineProfiler()
        profile.enable()  # 开始分析
        profile_wrap = profile(f)
        profile_wrap(*args, **kwargs)
        profile.disable()  # 停止分析
        profile.print_stats()  # 打印出性能分析结果
        return func_return
    return decorator

回归到外面的Flask中,具体的实例代码:

'''
Author: your name
Date: 2021-03-02 15:34:47
LastEditTime: 2021-03-07 21:57:51
LastEditors: 308711822@qq.com
Description: In User Settings Edit
FilePath: \py\main.py
'
''


from flask import Flask, Response
from flask import Flask, request


from line_profiler import LineProfiler
from functools import wraps
import time

# 查询接口中每行代码执行的时间


def line_profiler_func_line_time(f):
    @wraps(f)
    def decorator(*args, **kwargs):
        func_return = f(*args, **kwargs)
        profile = LineProfiler()
        profile.enable()  # 开始分析
        profile_wrap = profile(f)
        profile_wrap(*args, **kwargs)
        profile.disable()  # 停止分析
        profile.print_stats()  # 打印出性能分析结果
        return func_return
    return decorator


if __name__ == '__main__':
    app = Flask(__name__)

    @app.route("/")
    @line_profiler_func_line_time
    def ceshi():

        # // TODO:  标记代做事项1
        # eturn "你好"
        return "你好"

    def my_before_request():
        # // TODO:  标记代做事项2
        print('你大爷的')

    @app.errorhandler(Exception)
    def asa(e):

        return ''

    @app.route("/343")
    def ceshi2():

        print("ssssssssssss")

        # // TODO:  标记代做事项2
        return "你好"

    app.before_request(my_before_request)

    # app.run(port=1212, host='127.0.0.1', debug=True, threaded=False)

    app.run(port=1245, host='127.0.0.1', debug=True)


然后轻轻访问我们的接口:http://127.0.0.1:1245/ 最终输出的结果为:

你大爷的
Timer unit: 1e-07 s

Total time: 1.5e-06 s
File: d:\code\vscode\py\main.py
Function: ceshi at line 40

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    40                                               @app.route("/")
    41                                               @line_profiler_func_line_time
    42                                               def ceshi():
    43
    44                                                   # // TODO:  标记代做事项1
    45                                                   # eturn "你好"
    46         1         15.0     15.0    100.0          return "你好"

127.0.0.1 - - [07/Mar/2021 21:57:27] "GET / HTTP/1.1" 200 -
你大爷的
127.0.0.1 - - [07/Mar/2021 21:57:28] "GET /favicon.ico HTTP/1.1" 200 -

  • Line:代码行号
  • Hits:表示每行代码运行次数
  • Time:每行代码运行总时间(时间单位为微妙)
  • Per Hits:每行代码运行一次需要时间 (时间单位为微妙)
  • % Time:每行代码运行时间百分比
  • line Contents:每行代码内容

以上是关于如何在Flask中应用的line_profiler的示例,其实没什么好讲的。

说明

部分的图或资料来互联网收集整理,如有侵权,烦请联系,我会立即进行删除。

End

纯属个人实践中相关经验之谈,如有纰漏,还希望大佬们多多提点!小钟同学 | 文  【原创】| QQ:308711822


文章转载自小儿来一壶枸杞酒泡茶,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论