小编Ak4*_*k47的帖子

如何跟踪 Flask 中路线中每个功能所花费的时间?

现在我正在尝试捕获 Flask 中每个请求的统计信息,我能够捕获完成请求所需的时间。有没有办法捕获路线内每个功能所花费的时间。

MY Code capturing the time taken by a route
@app.teardown_request
def teardown_func(response):
    print("tearing down reqest")
    print("Request",request)
    required_data = {
        "path": request.full_path,
        "url": request.url,
        "json_data": request.get_json(),
        "start": request.start_time,
        "stop": dt.utcnow(),
        "total_elapsed_time": (dt.utcnow() - request.start_time).total_seconds()
    }
    print("request data",required_data)
    return response

def call_func():
    sleep(5)
    print("FunctionCalled")

def another_func():
    sleep(5)
    print("FunctionCalled2")


@app.route('/',methods=['GET','POST'])
def hello2():
    time.sleep(10)
    call_func()
    another_func()
    return 'Hello World'
Run Code Online (Sandbox Code Playgroud)

我如何计算 call_func() 和 another_func() 在执行该路由时花费了 5 秒的时间?

python logging flask python-2.7

1
推荐指数
1
解决办法
987
查看次数

标签 统计

flask ×1

logging ×1

python ×1

python-2.7 ×1