flask+gevent+uwsgi部署

2019-06-27 17:04:08 浏览数 (1)

先看flask默认的部署方式:
代码语言:javascript复制
#!/usr/bin/python3
# -*- encoding: utf-8 -*-
# app.py
from flask import Flask
from flask import jsonify
from flask import make_response

app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False


@app.route('/', methods=['GET'])
def index():
    data = {'project': 'api',
            'success': 'true',
            'message': 'Here Is Index.'}
    return make_response(jsonify(data))


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)

那么如何加入gevent uwsgi呢,按如下修改即可flask gevent uwsgi 部署:
代码语言:javascript复制
#!/usr/bin/python3
# -*- encoding: utf-8 -*-
# app.py
from flask import Flask
from flask import jsonify
from flask import make_response
import gevent.pywsgi # 导入相关的包
import gevent.monkey

gevent.monkey.patch_all()  # 可选内容,是否加载猴子补丁
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False


@app.route('/', methods=['GET'])
def index():
    data = {'project': 'api',
            'success': 'true',
            'message': 'Here Is Index.'}
    return make_response(jsonify(data))


if __name__ == '__main__':
    # app.run(debug=True, host='0.0.0.0', port=5000)  # 原flask默认部署
    gevent_server = gevent.pywsgi.WSGIServer(('0.0.0.0', 5000), app)
    gevent_server.serve_forever()
然后执行这个文件,flask服务器便部署成功,本文文件名为app.py,所以
代码语言:javascript复制
python app.py

运行成功 !

参考文献:

gevent-monkey-patching

gevent.WSGIServer使用

0 人点赞