The solution from @ sajid-siddiqi is technically correct, but keep in mind that the built-in WSGI server in Werkzeug (which is packaged in Flask and what it uses for app.run() ) is only single-threaded.
Install the WSGI server to be able to handle multithreaded behavior. I did some research on the various characteristics of the WSGI server. Your needs may vary, but if you use only Flask , I would recommend one of the following web servers.
For Python 2.x: gevent
You can install gevent via pip with the pip install gevent . Instructions on how to change your code accordingly can be found here: http://flask.pocoo.org/docs/0.10/deploying/wsgi-standalone/#gevent
For Python 3.x: meinheld
Gevent is better, but it is still not updated to use python3 (for updates see in this thread: https://github.com/gevent/gevent/issues/38 ). Of all the tests I reviewed, including real-world testing, meinheld seems to be the simplest and easiest WSGI server. (You can also take a look at uWSGI if you do not mind the additional configuration.)
You can also install meinheld via pip3 using the pip3 install meinheld . From there, look at the example provided in the meinheld source code for Flask integration: https://github.com/mopemope/meinheld/blob/master/example/flask_sample.py
* NOTE: Due to my use of PyCharm, the line from meinheld import server is highlighted as an error, but the server will work, so you can ignore the error.
mikeho Apr 27 '15 at 4:39 on 2015-04-27 04:39
source share