首页 > 解决方案 > 使用 return 语句正确退出 while 循环

问题描述

我的 Flask 路线之一中有这个 while 循环。该函数的目的是从用户那里检索一个值并检查该 str 是否存在于目录中的任何文件名中。如果为真,则返回“plot_list”并退出函数。如果为 false,请等待 30 秒,然后再次检查该 str 是否存在于目录中的任何文件名中。

@app.route("/data")
def datatable():
    selected_project = str(request.args.get('selected_project'))
    csv_path = "..\static\csv\dist_drift"

    start_time = time.time()
    while True:
        time.sleep(5)
        for fname in os.listdir(csv_path):
            if selected_project in fname:
                print("{} csv was found in {}".format(selected_project, csv_path))
                plot_list = ["basketball.html", "hockey.html"]
                return jsonify(plot_list)

            else:
                current_time = time.time()
                print("{} csv not found in {} after waiting {} seconds!".format(selected_project, csv_path,
                                                                                (current_time - start_time)))
                time.sleep(30)

这些是函数的打印语句:

Advanced csv was found in \static\csv\dist_drift
None csv not found in \static\csv\dist_drift after waiting 5.001329660415649 seconds!
["basketball.html", "hockey.html"]
None csv not found in \static\csv\dist_drift after waiting 35.00231146812439 seconds!
None csv not found in \static\csv\dist_drift after waiting 65.00326824188232 seconds!

我确信这是一个简单的解决方法,但我无法弄清楚。有什么帮助吗?

编辑:问题是即使 str 存在于目录中的文件名中,该函数也没有退出。

编辑2:这行得通。感谢您的提示。

@app.route("/data")
def datatable():
    selected_project = str(request.args.get('selected_project'))
    csv_path = "..\static\csv\dist_drift"

    start_time = time.time()
    time.sleep(5)
    while True:
        for fname in os.listdir(csv_path):
            if selected_project in fname:
                print("{} csv was found in {}".format(selected_project, csv_path))
                subprocess.call(
                    [r'C:\Program Files\SAS\JMP\15\jmp.exe',
                     r'C:\Users\Gol69206\PycharmProjects\rel-dashboard\JSL\Plots.jsl'])


                plot_list = ["basketball.html", "hockey.html"]
                return jsonify(plot_list)
            break
        else:
            current_time = time.time()
            print("{} csv not found in {} after waiting {} seconds!".format(selected_project, csv_path,
                                                                            (current_time - start_time)))
            time.sleep(30)

标签: python

解决方案


你在函数之外的回报。只需修复标签

@app.route("/data")
def datatable():
    selected_project = str(request.args.get('selected_Project'))
    csv_path = "..\static\csv\dist_drift"

    start_time = time.time()
    time.sleep(5)
    while True:
        for fname in os.listdir(csv_path):
            if selected_project in fname:
                print("{} csv was found in {}".format(selected_project , csv_path))
                plot_list = ["basketball.html", "hockey.html"]
                print(plot_list)
                return jsonify(plot_list)
        else:
            current_time = time.time()
            print("{} csv not found in {} after waiting {} seconds!".format(selected_project , csv_path,
                                                                            (current_time - start_time)))
            time.sleep(30)

推荐阅读