本地Flask服务器上的请求缓慢 [英] Slow Requests on Local Flask Server

查看:637
本文介绍了本地Flask服务器上的请求缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚开始在本地服务器上使用Flask,我注意到请求/响应时间比我觉得他们应该慢。



只是像下面这样简单的服务器需要接近5秒的时间来响应。

  from flask import Flask 

app = Flask(__ name__)

@ app.route(/)
def index():
returnindex

if __name__ ==__main__:
app.run()

任何想法?或者,这只是本地服务器是如何?

解决方案

好吧,我想通了。这似乎是与Werkzeug和os的支持ipv6的问题。



从Werkzeug网站 http://werkzeug.pocoo.org/docs/serving/


在支持ipv6的操作系统上进行配置,如现代Linux系统,OS X 10.4或更高版本以及Windows Vista,如果访问本地服务器,某些浏览器可能会非常慢。原因是有时候localhost被配置为在ipv4和ipv6 socktes上可用,一些浏览器将尝试首先访问ipv6,然后尝试访问ivp4。


所以解决的办法是从本地主机禁用ipv6,从我的主机文件中注释掉以下行:

  :: 1 localhost 

一旦我这样做,延迟问题就会消失。

我真的在挖掘Flask,我很高兴这个框架没有问题。我知道不可能是这样。


Just starting to play around with Flask on a local server and I'm noticing the request/response times are way slower than I feel they should be.

Just a simple server like the following takes close to 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 this just how the local server is?

解决方案

Ok I figured it out. It appears to be an issue with Werkzeug and os's that support ipv6.

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

On operating systems that support ipv6 and have it configured such as modern Linux systems, OS X 10.4 or higher as well as Windows Vista some browsers can be painfully slow if accessing your local server. The reason for this is that sometimes "localhost" is configured to be available on both ipv4 and ipv6 socktes and some browsers will try to access ipv6 first and then ivp4.

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

::1             localhost 

Once I do this the latency problems go away.

I'm really digging Flask and I'm glad that it's not a problem with the framework. I knew it couldn't be.

这篇关于本地Flask服务器上的请求缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆