1

The following is my code :

from flask import Flask

app = Flask(__name__)


@app.route('/hello/<value>')
def hello_world(value):

    if value == "1":
        while True:
            print('hello')
    else:
        return 'Hello ';


if __name__ == '__main__':
    app.run()

I wrote this code to see whether the flask app spawns a thread with each request or not.

I opened two tabs where in the first tab I passed the value as '1' and '2' to the other. The first blocked the tab and second displayed 'Hello' on the browser.

My query is whether each request to a flask server is run on a separate thread ? If yes why do we have the following feature in flask ?

app.run(threaded=True)

  • It can run threaded, if you run it as threaded (`app.run(threaded=True)`) but it will not run threaded by default. Same goes for the multiprocessing mode (`app.run(processes=True)`). However, both use a very basic WSGI gateway and are generally not recommended for production systems - use a dedicated WSGI container instead if you want to publish your web app. Flask documentation has a [dedicated part on the topic](http://flask.pocoo.org/docs/1.0/deploying/wsgi-standalone/). – zwer Aug 06 '18 at 07:50
  • If it does not run threaded by default, why did the request in the first tab block and the second tab served successfully. Doesn't this mean both requests are handled by independent threads ? (See that I have an infinite loop in the program if value is '1'). –  Aug 06 '18 at 10:18

0 Answers0