flask部署

flask部署的方式。

参考

服务器部署flask项目 (uwsgi部分参考)

https://github.com/unbit/uwsgi

https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html (包含了大部分的应用)

顺便记一下参考手册:

中文版

https://dormousehole.readthedocs.io/en/latest/quickstart.html#id16

英文版

https://flask.palletsprojects.com/en/2.0.x/

请求参数相关:

https://www.cnblogs.com/wsg-python/articles/9988295.html

部署

使用下面方式构建docker镜像

FROM python:3.6

RUN pip3 install requests \
    && pip3 install flask \
    && pip3 install uwsgi

备注:优化的方式是,写一个requirement.txt,然后使用docker构建。

构建

docker build -t scc/python:3.6 . 

运行镜像

docker run --rm -itd -p 8006:6000 -v $PWD:/mywork -w /mywork scc/python:3.6 uwsgi --ini uwsgi.ini

配置文件 uwsgi.ini

[uwsgi]
# localhost  port  端口自定义
#http=127.0.0.1:6000 
http=0.0.0.0:6000
# uwsgi 管理的项目入口文件
wsgi-file=/mywork/hello.py
# 管理的flask对象app
callable=app
touch-reload=/mywork/web

python 文件:app.py

import requests
from flask import Flask

for i in range(1,10+1):
    print("hello world %d"%i)

app = Flask(__name__)

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

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

uwsgi命令

# 启动后日志显示终端
uwsgi --ini uwsgi.ini
# 后台运行,日志输出到指定文件
uwsgi --ini uwsgi.ini --daemonize /var/log/uwsgi.log

备注

uwsgi.ini中的http,一般是暴露到内网,如果需要暴露到外网访问,需要更改。

另外,wsgi-file是启动的文件,是哪个文件,就写哪个文件。callable指定对应的是下面的变量名。

app = Flask(__name__)

touch-reload 目前还没有使用到过。

部署方式2

参见:Flask部署

app2.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "hello world"

进入到app2所在的文件夹,然后运行如下命令:

uwsgi --http 0.0.0.0:5000 --module app2:app

app2是文件,app是定义的Flask对象的变量名

也可以使用gunicorn

gunicorn -w 4 -b 0.0.0.0:5000 app2:app

uwsgi

该工具的常用配置

https://www.cnblogs.com/weiok/p/5719004.html

https://blog.csdn.net/chenggong2dm/article/details/43937433

uwsgi配置文件

cat /tmp/uwsgi.ini

[uwsgi]
http-socket = :80
plugin = python
chdir = /code/run/cms
wsgi-file = cms/wsgi.py
processes = 4
threads = 4
max-request = 20480
log-x-forwarded-for = true
logto = /code/uwsgi_web.log
stats = 127.0.0.1:9191

另外一个示例:

[uwsgi]
socket = 127.0.0.1:50000
chdir = /home/httpServer/
wsgi-file = httpServer/wsgi.py
processes = 4
stats = 127.0.0.1:9090
daemonize = /home/log/httpServer.log
pidfile = /tmp/uwsgi.pid
vacuum = true
log-maxsize = 50000000
disable-logging = true

ini格式说明

1,ini配置为 key=value 形式
2,在ini配置文件里,#号为注释,
3,布尔值为 true 和 false
4,在命令行里,uwsgi myconf.ini 等价于 uwsgi –ini myconf.ini

选项说明

  • socket : 地址和端口号,例如:socket = 127.0.0.1:50000
  • processes : 开启的进程数量
  • workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
  • chdir : 指定运行目录(chdir to specified directory before apps loading)
  • wsgi-file : 载入wsgi-file(load .wsgi file)
  • stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
  • threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
  • master : 允许主进程存在(enable master process)
  • daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
  • pidfile : 指定pid文件的位置,记录主进程的pid号。
  • vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
  • disable-logging : 不记录请求信息的日志。只记录错误以及uWSGI内部消息到日志中。如果不开启这项,那么你的日志中会大量出现这种记录:
    [pid: 347|app: 0|req: 106/367] 117.116.122.172 () {52 vars in 961 bytes} [Thu Jul 7 19:20:56 2016] POST /post => generated 65 bytes in 6 msecs (HTTP/1.1 200) 2 headers in 88 bytes (1 switches on core 0)

waitress

安装

pip install waitress

使用

waitress-serve --call 'flaskr:create_app'