4

I want my flask application to be able to process more than one call at the same time. I've been testing running with threaded=True or processes=3 with the code below but when I make two calls to the server the later always have to wait for the first one to complete. I know that it's recommended to deploy the application on a more sophisticated WSGI container but for now I just want my small app to be able to process 2 calls at once.

from flask import Flask, Response, stream_with_context
from time import sleep
app = Flask(__name__)

def text_gen(message):
    for c in message:
        yield c
        sleep(1)

@app.route("/")
def hello():
    stream = text_gen('Hello World')
    return Response(stream_with_context(stream))

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080, debug=True, threaded=True)
4
  • 3
    Your example works for me. How are you verifying that only one request is being process at a time? Two tabs in the same browser? That might be the browser holding of with multiple requests to the same domain. Commented Jan 6, 2014 at 21:43
  • app.run() launches a development server. It's not meant for production use. Commented Jan 6, 2014 at 21:48
  • 1
    Try inserting this after the yield in text_gen: import threading; print "Working hard in %s" % threading.currentThread().name Commented Jan 6, 2014 at 21:50
  • @LukasGraf Spot on, it was Chrome all the time. I added an answer below. Thanks. Commented Jan 6, 2014 at 22:46

1 Answer 1

4

@Lukas was right.

I was debugging in Google Chrome with two tabs. Apparently Chrome is trying to be smart by using the socket same for both tabs. How Chrome handles that can be changed with the -socket-reuse-policy flag when starting Chrome.

An easier way to test is by using different hostname in each tab or by using curl -N (-N flag for no buffer to see the streaming). Doing that it did indeed work as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.