通过pip下载包
requirements.txt
Flask
gunicorn
$ pip install -r ./src/requirements.txt -i \
https://mirrors.ustc.edu.cn/pypi/web/simple/
gunicorng配置文件( ,可以配置多个,启动时可以指定使用)
conf_*.py
#指定一些参数
bind = 5000
workers = 2
#server钩子
#服务启动完成之后,运行when_ready
def when_ready(server): # Called just after the server is started.
print 'when_ready 2'
pass
#服务结束时,运行on_exit
def on_exit(server):
print 'on_exit 1'
pass
其它还有很多钩子,参照 https://docs.gunicorn.org/en/stable/settings.html#server-hooks
web.py
#-*- coding: UTF-8 -*-
from flask import Flask ,request,url_for, abort,flash
@app.route("/")
def img1():
return json.dumps(obj=Result(0,'success').__dict__,ensure_ascii=False)
if __name__ == '__main__':
app.run()
启动服务
$ gunicorn -w 2 -b :5000 -c conf_dev.py web:app
-w = workers
-b= bind
-c= config file
web= start enter mudule