If you cant explain it simply, you dont understand it well enough

uwsgi 和 flask的使用

uwsgi 和 flask

1. 启动

$ uwsgi --socket 0.0.0.0:8002 --enable-threads -w manage:app 

*** Starting uWSGI 2.0.11.1 (64bit) on [Thu Aug  6 10:40:43 2015] ***
compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-4) on 22 July 2015 19:46:14
os: Linux-2.6.32-431.23.3.el6.x86_64 #1 SMP Thu Jul 31 17:20:51 UTC 2014
nodename: petool001Z
machine: x86_64
clock source: unix
detected number of CPU cores: 4
current working directory: /home/admin/cdoctor/petapi/src
detected binary path: /opt/gentoo/usr/local/python-2.7.8/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 1024
your memory page size is 4096 bytes
detected max file descriptor number: 65535
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address 0.0.0.0:8002 fd 3
Python version: 2.7.8 (default, Dec  3 2014, 10:14:25)  [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
Python main interpreter initialized at 0xb783b0
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72760 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
/opt/gentoo/usr/local/python-2.7.8/lib/python2.7/site-packages/setuptools-18.0.1-py2.7.egg/pkg_resources/__init__.py:1256: UserWarning: /home/admin/.python-eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
[2015-08-06 10:40:43,427] [DEBUG] [core.py:223] Initialize the cache
[2015-08-06 10:40:43,427] [DEBUG] [core.py:230] Initialize the simple database pool
[2015-08-06 10:40:43,427] [DEBUG] [core.py:234] Load all the defined handlers/view functions
[2015-08-06 10:40:43,428] [DEBUG] [core.py:181] /home/admin/cdoctor/petapi/src/petapi/api
petapi.api.ecs
ecs <module 'petapi.api.ecs' from './petapi/api/ecs.pyc'>
[2015-08-06 10:40:43,437] [DEBUG] [core.py:237] Application init done, start to serve

错误1:

invalid request block size: 21573 (max 4096)...skip

原因是:

连接:http://stackoverflow.com/questions/15878176/uwsgi-invalid-request-block-size

uwsgi --http 0.0.0.0:8002 --enable-threads -w manage:app

2. 配合Nginx的使用

步骤:

  • 启动uwsgi, 这里要用socket

    uwsgi --socket 0.0.0.0:8002 --enable-threads -w manage:app
    
  • 修改nginx配置文件

    server {
    listen 8001;
    server_name xxxxxx;
    
    location / {
            include uwsgi_params;
            uwsgi_pass unix:0.0.0.0:8002;
            }
    }
    
  • reload nginx

    $ sudo /etc/init.d/nginx reload
    Reloading nginx:                                           [  OK  ]
    
  • 连接测试

    $ curl -i "http://0.0.0.0:8001/test"
    HTTP/1.1 502 Bad Gateway
    Server: nginx/1.0.15
    Date: Thu, 06 Aug 2015 02:50:27 GMT
    Content-Type: text/html
    Content-Length: 173
    Connection: keep-alive
    
    <html>
    <head><title>502 Bad Gateway</title></head>
    <body bgcolor="white">
    <center><h1>502 Bad Gateway</h1></center>
    <hr><center>nginx/1.0.15</center>
    </body>
    </html>
    
  • 排查看log

    $ sudo tailf /var/log/nginx/error.log
    2015/08/06 10:46:54 [crit] 13609#0: *17 connect() to unix:0.0.0.0:8002 failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: xxxxxx, request: "GET /test HTTP/1.1", upstream: "uwsgi://unix:0.0.0.0:8002:", host: "0.0.0.0:8001"
    
  • nginx里面的conf问题写错

    uwsgi_pass unix:0.0.0.0:8002; # 这个应该去掉unix即可
    
  • 多线程

    uwsgi --socket 0.0.0.0:8002 --enable-threads -w manage:app --workers 4 --threads 4
    
  • 放在后台执行

    nohup /home/admin/gentoo/usr/local/python-2.7.8/bin/uwsgi --socket 0.0.0.0:8080 --enable-threads -w manage:app --workers 4 --threads 4 &
    

3. 重启服务

当修改代码的时候,必须要重启服务的,那怎么办呢?

posted @ 2015-08-06 21:00  zk47  阅读(710)  评论(0编辑  收藏  举报

I am a stupid bird, and I need to work hard