Queries cannot call multiple routes in a single Flask application

I cannot successfully use Python queries to call the second route in the same application using a checkbox. I know its best practice is to call the function directly, but I need it to invoke the url with requests. For example:

from flask import Flask import requests app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" # This works @app.route("/myrequest") def myrequest(): #r = requests.get('http://www.stackoverflow.com', timeout=5).text # This works, but is external #r = hello() # This works, but need requests to work r = requests.get('http://127.0.0.1:5000/', timeout=5).text # This does NOT work - requests.exceptions.Timeout return r if __name__ == "__main__": app.run(debug=True, port=5000) 
+7
python flask python-requests
source share
1 answer

Your code assumes that your application can process several requests at once: the initial request plus the request that is generated during the processing of the initial file.

If you use a development server, for example app.run() , it runs in a single thread by default; therefore, it can only process one request at a time.

Use app.run(threaded=True) to enable multiple threads on the development server.

+9
source share

All Articles