Slow requests to the local jar server

Just start playing with Flask on the local server, and I notice that the request / response time is slower than I think they should be.

Only a simple server, such as the next, takes about 5 seconds to respond.

from flask import Flask app = Flask(__name__) @app.route("/") def index(): return "index" if __name__ == "__main__": app.run() 

Any ideas? Or is it just like a local server?

+70
python flask
Jun 22 2018-12-12T00:
source share
8 answers

Ok, I figured it out. Apparently, the problem is with Werkzeug and os, which support ipv6.

From the Werkzeug website http://werkzeug.pocoo.org/docs/serving/ :

On operating systems that support ipv6 and are configured in the same way as modern Linux systems, OS X 10.4 or higher, and also on Windows Vista, some browsers can be very slow when accessing your local server. The reason for this is that sometimes "localhost" is configured to be accessible on both ipv4 and ipv6 socktes, and some browsers will try to access the first ipv6 and then ivp4.

So, the fix is ​​to disconnect ipv6 from localhost by commenting out the following line from my hosts file:

 ::1 localhost 

As soon as I do this, the problems with the delay will disappear.

I am really digging Flask, and I'm glad this is not a wireframe problem. I knew this could not be.

+83
Jun 22 2018-12-12T00:
source share

Add "threaded = True" as an argument to app.run () as suggested here: http://arusahni.net/blog/2013/10/flask-multithreading.html

For example: app.run(host="0.0.0.0", port=8080, threaded=True)

The ipv6-disabling solution did not work for me, but it did.

+77
Feb 28 '15 at 0:30
source share

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.

+9
Apr 27 '15 at 4:39 on
source share

I don't have enough reputation for comment, so I will add this as a “solution”. My problem was solved with "threaded = True", but I want to give some background to distinguish my problem from others for which it might not.

  • My problem only occurred when running Flask with python3. By switching to python2, I no longer had this problem.
  • My problem only appeared when accessing the api with Chrome, after which Chrome displayed the expected screen, but everything else hung (waving, ffx, etc.) until I rebooted or closed the Chrome tab, after which everything else that was waiting, returned the result.

My best guess is that Chrome tried to keep the session open, and Flask blocked subsequent requests. As soon as the connection to Chrome was stopped or reset, all the rest were processed.

In my case, threading fixed it. Of course, now I am browsing through some of the links that others have provided to make sure that this does not cause any other problems.

+5
Jul 01 '16 at 18:01
source share

threaded=True works for me, but finally I realized that the problem is with foxyproxy on firefox. Since when the flash drive application is running on the local host, a slow response occurs if

  • foxyproxy is included in firefox

slow response will not happen if

  • foxyproxy is disabled in firefox

  • access the website using other browsers

The only solution I found was to disable foxyproxy, try to add localhost to the blacklist and configure the settings, but none of them worked.

+3
Jun 18 '17 at 18:46
source share

I used Micheco's answer to solve my problem.

::1 localhost already commented out in my hosts file, and setting Threaded=true didn't work for me. Each REST request took 1 second, not instantaneous.

I use Python 3.6, and I got a flash drive that will be fast and responsive to REST requests, forcing the flask to use gevent as WSGI.

To use gevent, install it with pip install gevent

After that, I used https://gist.github.com/viksit/b6733fe1afdf5bb84a40#file-async_flask-py-L41 to configure the jar to use gevent.

Incase link goes down, here are the important parts of the script:

 from flask import Flask, Response from gevent.pywsgi import WSGIServer from gevent import monkey # need to patch sockets to make requests async # you may also need to call this before importing other packages that setup ssl monkey.patch_all() app = Flask(__name__) # define some REST endpoints... def main(): # use gevent WSGI server instead of the Flask # instead of 5000, you can define whatever port you want. http = WSGIServer(('', 5000), app.wsgi_app) # Serve your application http.serve_forever() if __name__ == '__main__': main() 
+1
Mar 10 '19 at 8:44
source share

I got this error when starting on hosts other than localhost , so for some of them the same symptoms may occur as other major problems.

I switched most of the things I used in Tornado, and, oddly enough, it helped. I had a few slow page loads, but everything seems more responsive. It is also very anecdotal, but I seem to notice that Flask alone will slow down over time, but Flask + Tornado is smaller. I assume that using Apache and mod_wsgi will improve the situation, but Tornado is really easy to set up (see http://flask.pocoo.org/docs/deploying/others/ ).

(Also, a related question: the flask application sometimes hangs )

0
Dec 28
source share

I had a different solution here. I just deleted all .pyc from the server directory and started it again. By the way, localhost has already been commented out in my hosts file (Windows 8).

The server froze all the time, and now it works fine.

0
May 7 '14 at 5:18
source share



All Articles