1、安装gunicorn
pip install Gunicorn
2、为了更好的管理gunicorn,在项目目录下创建gunicorn_conf.py文件,内容如下:
import os
import multiprocessing
path_of_current_file = os.path.abspath(__file__)
path_of_current_dir = os.path.split(path_of_current_file)[0]
chdir = path_of_current_dir
workers = 3 # 进程数量
worker_class = 'sync'
pidfile = '%s/gunicorn.pid' % path_of_current_dir # 存放Gunicorn进程pid的位置,便于跟踪
accesslog = '%s/log/00_gunicorn_access.log' % path_of_current_dir # 存放访问日志的位置,注意首先需要存在logs文件夹,Gunicorn才可自动创建log文件
errorlog = '%s/log/00_gunicorn_access.log' % path_of_current_dir # 存放错误日志的位置,可与访问日志相同
reload = True # 如果应用的代码有变动,work将会自动重启,适用于开发阶段
daemon = True # 是否后台运行
debug = False
timeout = 30 # server端的请求超时秒数
loglevel = 'error'
3、利用gunicorn 启动flask 服务。
# 开启服务
/home/admin/anaconda3/envs/spam_nlp/bin/gunicorn -b 10.4.231.108:7100 weixin_qq_email_url_api:app -c gunicorn_conf.py --limit-request-line 0
4、杀死服务进程
# 关闭服务
ps -ef | grep weixin_qq_email_url_api | awk '{print $2}'|xargs kill -9
5、查看进程
# 查看进程
ps -aux | grep gunicorn / gunicorn.pid