Gunicorn运行flask

Gunicorn“绿色独角兽”是一个被广泛使用的高性能的Python WSGI UNIX HTTP服务器,移植自Ruby的独角兽(Unicorn )项目,使用pre-fork worker模式,具有使用非常简单,轻量级的资源消耗,以及高性能等特点。

Gunicorn运行flask_第1张图片


安装gunicorn

pip install gunicorn

运行gunicorn

$ gunicorn [OPTIONS] 模块名:变量名

模块名是python文件名,可以是完整的路径+python文件名;变量名是python文件中可调用的WSGI(Web Server Gateway ).

直接运行:

#直接运行,默认启动的127.0.0.1::8000
gunicorn 运行文件名称:Flask程序实例名

指定进程和端口号: -w: 表示进程(worker)。 -b:表示绑定ip地址和端口号(bind)。

gunicorn -w 4 -b 127.0.0.1:5001 运行文件名称:Flask程序实例名

我的项目主入口manage.py:

import os

from flask_script import Manager
from flask_migrate import MigrateCommand

from App import create_app

# 环境变量,获取当前Flask环境
env = os.environ.setdefault('FLASK_ENV','devlop')

# 生成app
app = create_app(env)
manager = Manager(app)

# 给manage.py 添加数据库迁移的命令
manager.add_command('db',MigrateCommand)

if __name__ == '__main__':
    manager.run()

所以运行方式为:

gunicorn -w 4 -b 127.0.0.1:5001 manage:app

gunicron部署flask

不过在production环境,起停和状态的监控最好用supervisior之类的监控工具,然后在gunicorn的前端放置一个http proxy server, 譬如nginx。

下面是supervisor和nginx的配置。

# supervisor

你可能感兴趣的:(Flask,flask,gunrcorn)