为什么80%的码农都做不了架构师?>>>
1. 环境准备python2.7下
(1)安装nginx
(2) 安装python
(3) pip install gevent flask gunicorn
2. flask代码
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = '' __author__ = '' __mtime__ = '15/06/2017' """ from flask import Flask app = Flask(__name__) @app.route("/") def main(): return "hello world!" if __name__ == '__main__': app.run()
3. gunicorn配置
cat gun.conf
import os bind='127.0.0.1:5000' workers=4 backlog=2048 worker_class="gevent" #sync, gevent,meinheld daemon=True # 是否后台运行 debug=True pidfile='./gunicore.pid' loglevel='debug' # debug error warning error critical accesslog='log/gunicorn.log' errorlog='log/gunicorn.err.log'
4. nginx 配置
cat /usr/local/nginx/conf/nginx.conf
# another virtual host using mix of IP-, name-, and port-based configuration # server { listen 80; # listen somename:8080; server_name localhost; location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_pass http://127.0.0.1:5000; root html; index index.html index.htm; } }
5. 运行
gunicorn -c gun.conf flaskr:app
6. 测试
time ab -n 1000 -c 100 http://127.0.0.1/
gunicorn 是多进程运行的1000个每100个并发执行大概1秒多