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).
python flask
add a comment |
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).
python flask
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 thesearch/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
add a comment |
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).
python flask
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
python flask
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 thesearch/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
add a comment |
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 thesearch/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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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