3

I have the following flask server:

from flask import Flask

app = Flask(__name__)

@app.route("/test")
def test():
    return 'OK'

app.run()

If I run this with python main.py and go to localhost:5000/test in Chrome, sometimes the request completes in 5ms but sometimes it is "stalled" for 300ms.

Flask network traffic

This seems more likely to happen if I wait between requests (a few minutes) or if I do a hard refresh in Chrome.

I am using:

  • Flask: 1.0.2
  • Chrome: v72 (with extensions disabled)
  • OS: Windows 10

Anyone know what could be causing this or how to fix it (while still using Flask's dev server)?

EDIT I tried using 127.0.0.1 (as suggested by Sraw) and the 300ms delay went away. I'm still curious why this isn't working with localhost though.

rob
  • 17,995
  • 12
  • 69
  • 94
  • this will help you [Slow Requests on Local Flask Server](https://stackoverflow.com/questions/11150343/slow-requests-on-local-flask-server) – sahasrara62 Feb 19 '19 at 22:46
  • I tried the first 2 solutions from @prashantrana's link but I'm still seeing the same issue – rob Feb 19 '19 at 23:25
  • 4
    Have you tried using `127.0.0.1` instead of `localhost`? – Sraw Feb 19 '19 at 23:32
  • @Sraw ah the problem goes away when I use 127.0.0.1! – rob Feb 19 '19 at 23:42
  • 1
    Cool! That's an interesting problem about DNS resolving. Your OS is trying to use ipv6 address for `localhost` first but fail. Then fallback to ipv4 address. – Sraw Feb 19 '19 at 23:44
  • @rob ,it's great your problem is solved now . you can also use accepted solution by OP from #Meroon is solving the issue where he disable ipv6 from the localhost by commenting out the following line from hosts file. with this insteal of using 127.0.0.1 , you can use localhost – sahasrara62 Feb 20 '19 at 05:55
  • 1
    I wouldn't say my issue is solved. Using 127.0.0.1 is a good workaround but I'm building a dev tool that others will run on localhost and see this issue. This line in the host file "::1 localhost" is commented out by default in windows 10 so that fix did not work. – rob Feb 20 '19 at 14:34

0 Answers0