【问题标题】:Flask: code inside a @app.route fails (runs forever) when called a second timeFlask:@app.route 中的代码在第二次调用时失败(永远运行)
【发布时间】:2019-04-21 12:36:19
【问题描述】:

我在 Flask 路由中执行了一些 Python metapy 代码,该路由在第一次调用路由时运行良好(即用户在应用程序启动后提交表单),但在第二次运行时不会终止(即应用程序启动后再次提交表单)。

准确地说:

@app.route('/search', methods=['POST'])
def searchPageResults():
    form = SearchForm(request.form)
    import metapy
    idx = metapy.index.make_inverted_index(os.path.abspath("search/config.toml"))
    ranker = metapy.index.OkapiBM25()
    query = metapy.index.Document()
    query.content("auto")
    for result in ranker.score(idx, query):
         print(result)
    return render_template('SearchPage.html', form=form)

如果我在 Flask 外部运行,方法内部的代码 sn-p 运行良好(无论我调用多少次)。只有在用 @app.route(...) 装饰的方法内部,它似乎只运行一次。具体来说:ranker.score(...) 函数是永远运行的函数。 由于代码在烧瓶外运行良好,我认为在我不明白的背景中发生了一些烧瓶特定的事情。

到目前为止我尝试了什么(但没有帮助):

  • 当我在文件顶部有“import metapy”语句时, 那么即使是第一次调用 ranker.score(...) 也会永远运行。
  • 我确保“import metapy”以及“idx”和“ranker”的初始化只运行一次,方法是将搜索功能放在自己的类中 在 Flask 服务器启动时实例化。然而,也那时 即使在第一次调用路由时,代码也不会运行。

Flask 是否有特定的东西来解释这种行为?

----更新:附加信息----- config.toml

index = "idx"
corpus = "line.toml"
dataset = "data"
prefix = "."
stop-words = "search/german-stopwords.txt"
start-exceptions = "search/sentence-start-exceptions.txt"
end-exceptions = "search/sentence-end-exceptions.txt"
function-words = "search/function-words.txt"
punctuation = "search/sentence-punctuation.txt"

[[analyzers]]
method = "ngram-word"
ngram = 1
filter = [{type = "icu-tokenizer"}, {type = "lowercase"}]

如前所述,该行为仅在此 Flask 路由的第二次调用之后发生。本地一切正常(使用完全相同的数据集和 config.toml)

更新:MetaPy Flask 演示应用中的行为相同 我在 MetaPy 演示应用程序中有相同的行为:https://github.com/meta-toolkit/metapy-demos。 (唯一不同的是,由于可用性的原因,我需要为一些包使用 requirements.txt 中指定的一些较新版本)。

【问题讨论】:

  • 你能粘贴你的错误堆栈跟踪吗?
  • 无错误 - 只有 ranker.score(...) 永远运行。 :-(
  • 这很有趣,您能否提供一个search/config.toml 的示例,以便我在本地查看一下?
  • 当然。你去:index = "idx" corpus = "line.toml" dataset = "data" prefix = "." stop-words = "search/german-stopwords.txt" start-exceptions = "search/sentence-start-exceptions.txt" end-exceptions = "search/sentence-end-exceptions.txt" function-words = "search/function-words.txt" punctuation = "search/sentence-punctuation.txt" [[analyzers]] method = "ngram-word" ngram = 1 filter = [{type = "icu-tokenizer"}, {type = "lowercase"}]

标签: python flask


【解决方案1】:

解决了。 Flask 集成网络服务器出现问题。一旦部署到另一个网络服务器,问题就解决了。

【讨论】:

    猜你喜欢
    • 2011-01-20
    • 2015-12-01
    • 2011-09-02
    • 2015-12-08
    • 2017-05-20
    • 1970-01-01
    • 2021-04-02
    • 2010-12-21
    • 1970-01-01
    相关资源
    最近更新 更多