Question: Question:
I am making a server application with Python 3.3.5 + Flask.
Contents
When I send an HTTP request to Flask, it immediately returns a response,
- Send two or more requests at the same time
or
- Continue to send requests while processing the earlier request
If you do something like that, the response of the slower request will be waited until the response to the earlier one is completed.
I think it's because it's processed by a single thread, but is there a way to do this in parallel with Flask?
Code used for testing
if __name__ == '__main__':
app = Flask(__name__)
app.config.update(PROPAGATE_EXCEPTIONS = True)
@app.route('/')
def greet():
n = randrange(1, 10)
sleep(n)
return 'Hello world! I slept {0}[s].'.format(str(n))
app.run()
This is the code to wait 1-10 seconds based on the random number and return how many seconds you waited when the request flew.
If you prepare two browser tabs and access localhost: 5000 quickly for both, you can see from the waiting time that the waiting phase starts after the response is returned to the person who accessed first.
If Flask can return responses in parallel, the smaller random number in the wait time should come back first.
Answer: Answer:
Flask.run () is a development server, not intended to be used to actually provide services. For general configurations,
- Run static files with nginx, reverse proxy from nginx and run Flask apps with gunicorn or uWSGI.
- Use Apache and mod_wsgi.
There are options such as. I personally recommend the former.