Gunicorn通过Vue参考显着降低了烧瓶执行速度

时间:2018-05-15 19:54:28

标签: python flask vue.js gunicorn pipenv

我使用Gunicorn运行一个非常基本的Flask应用程序(通过pipenv),该应用程序由Vue网站引用(基本上它并不比这个MCVE复杂得多)。

Pipfile

[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[dev-packages]

[packages]
flask = "*"
flask-cors = "*"
flask-pymongo = "*"
gunicorn = "*"

[requires]
python_version = "3.5"

wsgi.py

from helloworld import application

if __name__ == "__main__":
    application.run()

helloworld.py

from flask import Flask
from flask_cors import CORS
from flask_pymongo import PyMongo

import json

application = Flask(__name__)
CORS(application)

application.config['MONGO_DBNAME'] = 'helloworld'
mongo = PyMongo(application, config_prefix='MONGO')

@application.route("/greet")
def hello():
    someone = mongo.db.people.find_one({},{"_id":0})
    return json.dumps(someone)

if __name__ == "__main__":
    application.run(host='0.0.0.0', port=5432)

的index.html

<!DOCTYPE html>
<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello</h1>
    <p class="paragraph" id="app">{{ greeted }}!</p>
  </body>
</html>
<script>
  new Vue({
    el: '#app',
    data: {
      greeted: ''
    },
    created () {
      fetch('http://0.0.0.0:5432/greet')
        .then(response => response.json())
        .then(json => {this.greeted = json.name})
    }
  })
</script>

(对mongo.db.people.find_one({},{"_id":0})的调用会返回{"name":"World"}

当我使用Flask开发服务器运行时:

pipenv run python helloworld.py

网站在硬刷新时快速获得价值。但是,如果我通过Gunicorn运行它:

pipenv run gunicorn -w 3 -b 0.0.0.0:5432 wsgi

返回数据可能需要几个秒(有时是几分钟)。

导致此行为的原因是什么?

作为参考,this问题与标题类似,但有不同的问题。

编辑:如果我让服务器运行足够长时间,我会收到以下消息:

[CRITICAL] WORKER TIMEOUT (pid:XXXXX)

编辑2 :将工作人员类型更改为gevent似乎可以解决问题:

pipenv run gunicorn --worker-class gevent -w 3 -b 0.0.0.0:5432 wsgi

我不确定为什么(我没有做任何会导致阻塞数十秒的事情)。此外,更改为gevent会导致此警告:

  

/home/user/.local/share/virtualenvs/FlaskApp-XXXXX/lib/python3.5/site-packages/gunicorn/workers/ggevent.py:65:MonkeyPatchWarning:ssl之后的猴子修补ssl已经导入可能会导致错误,包括Python 3.6上的RecursionError。请早点修补猴子。见https://github.com/gevent/gevent/issues/1016     monkey.patch_all(子= TRUE)

0 个答案:

没有答案