将Flask应用程序部署在nginx,tornado的简单方法
前言
这不是一个好方法.但是可以用\简单\性能将就\Windows支持.大神可以参考官方文档进行部署.
flask代码:main.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(debug=True)
tornado 代码:run.py
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
#导入flask项目
from main import app
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5000)#对应flask的端口
IOLoop.instance().start()
#如果要开启多进程模式用下面的代码,不过仅在linux下
# http_server = HTTPServer(WSGIContainer(app))
# http_server.bind(8888)
# http_server.start(0)
# IOLoop.instance().start()
Windows 不支持多进程解决方案(重复启动,监听多端口)
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.options import define, options
import tornado.options
#导入flask项目
from main import app
define("port", default=5000, help="run on the given port", type=int)
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(options.port)#对应flask的端口
IOLoop.instance().start()
windows 启动脚本
python run.py --port=5001
python run.py --port=5002
python run.py --port=5003
修改nginx配置文件:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:5000; #添加这句,指向flask
}
}
window 解决方案
upstream run {
server localhost:5001;
server localhost:5002;
server localhost:5003;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://run #添加这句,指向flask
}
}
启动 run.py 和 nginx
搞定收工,可以打开浏览器看一下效果,也可以用apache-jmeter 测试一下.
(完)