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











up vote
0
down vote

favorite












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



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). Only inside the method decorated with @app.route(...) it seems to only run once. To be specific: the ranker.score(...) function is the one running forever.
Since the code runs fine outside flask, I think there is something Flask specific happening in the background I don't understand.



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.

  • 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. However, also then the
    code won't run even at the first call of the route.


Is there something Flask specific explaining this behaviour?



----Update: additional info-----
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. Locally everything works fine (with exact same dataset and 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. (Only difference is that I needed to take some newer versions as specified in the requirements.txt for some packages due to availability).










share|improve this question
























  • Can you paste your error stacktrace?
    – Daniel Däschle
    Nov 19 at 8:38










  • No Error - only the ranker.score(...) runs forever. :-(
    – MaxB
    Nov 19 at 14:59










  • This is interesting, could you include an example of the search/config.toml so I can take a look locally?
    – Luis Orduz
    Nov 19 at 18:53










  • Sure. There you go: 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"}]
    – MaxB
    Nov 19 at 19:02

















up vote
0
down vote

favorite












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



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). Only inside the method decorated with @app.route(...) it seems to only run once. To be specific: the ranker.score(...) function is the one running forever.
Since the code runs fine outside flask, I think there is something Flask specific happening in the background I don't understand.



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.

  • 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. However, also then the
    code won't run even at the first call of the route.


Is there something Flask specific explaining this behaviour?



----Update: additional info-----
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. Locally everything works fine (with exact same dataset and 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. (Only difference is that I needed to take some newer versions as specified in the requirements.txt for some packages due to availability).










share|improve this question
























  • Can you paste your error stacktrace?
    – Daniel Däschle
    Nov 19 at 8:38










  • No Error - only the ranker.score(...) runs forever. :-(
    – MaxB
    Nov 19 at 14:59










  • This is interesting, could you include an example of the search/config.toml so I can take a look locally?
    – Luis Orduz
    Nov 19 at 18:53










  • Sure. There you go: 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"}]
    – MaxB
    Nov 19 at 19:02















up vote
0
down vote

favorite









up vote
0
down vote

favorite











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



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). Only inside the method decorated with @app.route(...) it seems to only run once. To be specific: the ranker.score(...) function is the one running forever.
Since the code runs fine outside flask, I think there is something Flask specific happening in the background I don't understand.



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.

  • 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. However, also then the
    code won't run even at the first call of the route.


Is there something Flask specific explaining this behaviour?



----Update: additional info-----
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. Locally everything works fine (with exact same dataset and 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. (Only difference is that I needed to take some newer versions as specified in the requirements.txt for some packages due to availability).










share|improve this question















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



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). Only inside the method decorated with @app.route(...) it seems to only run once. To be specific: the ranker.score(...) function is the one running forever.
Since the code runs fine outside flask, I think there is something Flask specific happening in the background I don't understand.



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.

  • 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. However, also then the
    code won't run even at the first call of the route.


Is there something Flask specific explaining this behaviour?



----Update: additional info-----
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. Locally everything works fine (with exact same dataset and 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. (Only difference is that I needed to take some newer versions as specified in the requirements.txt for some packages due to availability).







python flask






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 20:19

























asked Nov 19 at 7:03









MaxB

33




33












  • Can you paste your error stacktrace?
    – Daniel Däschle
    Nov 19 at 8:38










  • No Error - only the ranker.score(...) runs forever. :-(
    – MaxB
    Nov 19 at 14:59










  • This is interesting, could you include an example of the search/config.toml so I can take a look locally?
    – Luis Orduz
    Nov 19 at 18:53










  • Sure. There you go: 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"}]
    – MaxB
    Nov 19 at 19:02




















  • Can you paste your error stacktrace?
    – Daniel Däschle
    Nov 19 at 8:38










  • No Error - only the ranker.score(...) runs forever. :-(
    – MaxB
    Nov 19 at 14:59










  • This is interesting, could you include an example of the search/config.toml so I can take a look locally?
    – Luis Orduz
    Nov 19 at 18:53










  • Sure. There you go: 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"}]
    – MaxB
    Nov 19 at 19:02


















Can you paste your error stacktrace?
– Daniel Däschle
Nov 19 at 8:38




Can you paste your error stacktrace?
– Daniel Däschle
Nov 19 at 8:38












No Error - only the ranker.score(...) runs forever. :-(
– MaxB
Nov 19 at 14:59




No Error - only the ranker.score(...) runs forever. :-(
– MaxB
Nov 19 at 14:59












This is interesting, could you include an example of the search/config.toml so I can take a look locally?
– Luis Orduz
Nov 19 at 18:53




This is interesting, could you include an example of the search/config.toml so I can take a look locally?
– Luis Orduz
Nov 19 at 18:53












Sure. There you go: 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"}]
– MaxB
Nov 19 at 19:02






Sure. There you go: 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"}]
– MaxB
Nov 19 at 19:02



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369759%2fflask-code-inside-a-app-route-fails-runs-forever-when-called-a-second-time%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369759%2fflask-code-inside-a-app-route-fails-runs-forever-when-called-a-second-time%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?