简体   繁体   English

VS Code Flask 调试运行比从命令行运行 Flask 慢得多(具有相同的参数)

[英]VS Code Flask debug running much slower compared to running Flask from command line (with same parameters)

I am currently in early developing stages of a flask app in VS Code and I don't understand why I have huge runtime differences between starting the flask app using the VS Code debugger and when I start my flask app from the command line(5 to 6 times slower in VS code debugging), but with the same flask parameters.我目前处于flask APP的早期阶段,我不明白为什么我在启动Z319C3206A7F10C17C17C3B91116D4A954A957646Z APP1311313131113B的运行时差异很大。 VS 代码调试慢 6 倍),但使用相同的 flask 参数。 This seems to mainly happen when I use pandas/numpy objects.这似乎主要发生在我使用 pandas/numpy 对象时。

I have the following content in my launch.json (only content relevant to "Python: Flask"我的launch.json中有以下内容(仅与“Python:Flask”相关的内容

        {
            "name": "Python: Flask",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "test_flask_app.py",
                "FLASK_ENV": "development",
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload",
            ],
            "jinja": true
        },

I then click on "Start debugging" and the following is displayed in the terminal:然后我单击“开始调试”,终端中显示以下内容:

 * Serving Flask app "test_flask_app.py"
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

When I start Flask from the command line with the following commands: export FLASK_APP=test_flask_app.py export FLASK_ENV=devlopment flask run --no-debugger --no-reload当我使用以下命令从命令行启动 Flask 时: export FLASK_APP=test_flask_app.py export FLASK_ENV=devlopment flask run --no-debugger --no-reload

The following information is displayed on startup (identical to the first one):启动时显示以下信息(与第一个相同):

 * Serving Flask app "test_flask_app.py"
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

The strange thing is that the runtime time of one page takes about 800ms when I run Flask directly from the terminal and 5 to 6 seconds when I run Flask from VS Code debugger.奇怪的是,当我直接从终端运行 Flask 时,一页的运行时间大约需要 800 毫秒,而当我从 VS Code 调试器运行 Flask 时,需要 5 到 6 秒。

I believe all my parameters are identical (and I can of course start my Flask manually), but I would like to understand why there is such a big difference in runtimes?我相信我的所有参数都是相同的(我当然可以手动启动我的 Flask),但我想了解为什么运行时会有如此大的差异?

Here is the sample code I used:这是我使用的示例代码:

import time
import numpy as np
import pandas as pd
from flask import Flask

# vectorized function
def generateHeaderObject(text, level, calc, axis):
    return "." * int(level) + text + '|' + str(False) + '|' + "" + '|' + \
        "hdr " + axis + "ht" + (" " + axis + "agg" if calc else "")

# create sample dataframe
n = 1000000
df = pd.DataFrame(dict(
    calc=np.random.choice(a=[False,True],size=n),
    level=np.random.randint(1, 12, size=n),
    id=np.random.randint(10000000, 99999999, size=n)
))
df['id'] = df['id'].astype(str)

app = Flask(__name__) 

@app.route('/') 
# ‘/’ URL is bound with hello_world() function. 
def main():
    # Calculate new column
    start = time.time()
    df['output'] = np.vectorize(generateHeaderObject)(
        df['id'], df['level'], df['calc'], "r"
    )
    return 'runtime np.vectorize (ms): ' + str((time.time() - start) * 1000)

This is simply overhead from using the debugger due to it having to run within your Flask application.这只是使用调试器的开销,因为它必须在您的 Flask 应用程序中运行。 If you use "Run without Debugging" in VS Code you won't see the slowdown.如果您在 VS Code 中使用“不带调试运行”,您将不会看到减速。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM