简体   繁体   English

Flask:第二次调用时,@ app.route中的代码失败(永远运行)

[英]Flask: code inside a @app.route fails (runs forever) when called a second time

I have some Python metapy code being executed inside a Flask route which runs perfectly fine when the route is called the first time (ie a user submits a form after startup of the application) but it doesnt terminate when it runs a second time (ie the form is submitted a second time after application startup). 我在Flask路由中执行了一些Python metapy代码,该路由在第一次调用时运行得很好(即,用户在应用程序启动后提交了一个表单),但是在第二次运行时并没有终止(例如,表单在应用程序启动后第二次提交)。

Precisely: 正是:

@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)

The code snippet inside the method runs fine if I run it outside Flask (no matter how many times I call it). 如果我在Flask外部运行该方法,则该方法内部的代码段将运行良好(无论调用多少次)。 Only inside the method decorated with @app.route(...) it seems to only run once. 仅在用@ app.route(...)装饰的方法内部,它似乎只能运行一次。 To be specific: the ranker.score(...) function is the one running forever. 具体来说:ranker.score(...)函数是永远运行的函数。 Since the code runs fine outside flask, I think there is something Flask specific happening in the background I don't understand. 由于代码在烧瓶外部运行良好,因此我认为我不了解在后台发生了Flask特定的事情。

What I tried so far (but didn't help): 到目前为止,我尝试了什么(但没有帮助):

  • When I have the "import metapy" statement at the top of the file, then even the first call to ranker.score(...) runs forever. 当我在文件顶部显示“ import metapy”语句时,即使是对ranker.score(...)的首次调用也将永远运行。
  • I ensured that "import metapy" and the initialization of "idx" and "ranker" only run once by putting the search functionality inside an own Class which is instantiated at Flask server startup. 我通过将搜索功能放在自己的Class中(在Flask服务器启动时实例化),确保“ import metapy”以及“ idx”和“ ranker”的初始化仅运行一次。 However, also then the code won't run even at the first call of the route. 但是,即使在路由的第一次调用时,代码也不会运行。

Is there something Flask specific explaining this behaviour? Flask是否有特定的东西可以解释这种行为?

----Update: additional info----- config.toml ----更新:附加信息----- 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"}]

As said, the behaviour only occurs after the second call of this Flask route. 如前所述,该行为仅在该Flask路由的第二次调用之后发生。 Locally everything works fine (with exact same dataset and config.toml) 在本地,一切正常(使用完全相同的数据集和config.toml)

Update: same behaviour in MetaPy Flask demo app I have the same behaviour in the MetaPy demo app: https://github.com/meta-toolkit/metapy-demos . 更新:MetaPy Flask演示应用程序中的行为相同我在MetaPy演示应用程序中的行为相同: https : //github.com/meta-toolkit/metapy-demos (Only difference is that I needed to take some newer versions as specified in the requirements.txt for some packages due to availability). (唯一的区别是由于可用性,我需要采用一些包的requirements.txt中指定的较新版本)。

Solved. 解决了。 There was a problem with the Flask integrated Webserver. Flask集成Web服务器出现问题。 Once deployed to another webserver, the problem was solved. 一旦部署到另一个Web服务器,问题就解决了。

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

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