python flask threaded true not working

14,611

Solution 1

From the flask documentation about Deployment Options

While lightweight and easy to use, Flask’s built-in server is not suitable for production as it doesn’t scale well and by default serves only one request at a time. Some of the options available for properly running Flask in production are documented here.

That's why your second request isn't happening until the first is complete, because the flask server on it's own can only handle one request at a time. To solve this you will need to run Flask on some kind of deployment server for example gunicorn or uWSGI which seem to be the most popular.

You also might find the answer to this or this question helpful. Deployment Options also has a lot of links to guides and information about different ways of solving your issue.

Solution 2

I cant seem to find my reference but I recall reading from another SO post why two tabs from the same browser fail when requesting form the same route despite threaded=True.

The gist of the post was that while threading will handle multiple connections to the same app better, the browser itself will try to reuse any already open connections. So with your two tab setup you could request from different routes and it work fine but if you request from the same route youll run into an issue because of the recycling of connections by the browser. Try opening two different browsers and testing this. Again I apologize for not having my original reference, I would like to give the author credit. If I find it Ill update this post.

Share:
14,611
Vishnu
Author by

Vishnu

Updated on June 04, 2022

Comments

  • Vishnu
    Vishnu almost 2 years

    With threaded=True requests are each handled in a new thread. But if I set threaded true to my application it is showing unknown behavior.

    This is my code.

    from flask import Flask
    from flask import jsonify
    import time
    
    app = Flask("proxapp")
    
    import datetime
    
    @app.route('/slow')
    def slow():
        start = datetime.datetime.now()
        time.sleep(10)
        return jsonify(start = start, end = datetime.datetime.now())
    
    
    try:
        app.run(threaded=True)
    except Exception, e:
        print repr(e)
    

    I have opened two tabs in windows and tried to request same url in different tabs. second request is being served only after first request is being served. Second request is taking ~20 seconds to be served.

    What is the problem with my code?