zoukankan      html  css  js  c++  java
  • 快速开始Python/WSGI应用程序

    快速开始Python—wsig应用程序

    官方参考文档

    安装 uwsgi

    • 安装
    pip install uwsgi
    uwsgi --version  # 查看 uwsgi 版本
    
    • 测试 uwsgi 是否正常

    新建一个测试文件 helloworld.py

    def application(env, start_response):
        start_response('200 OK', [('Content-Type', 'text/html')])
        return [b'hello world',]
    
    • 启动并监听 9090 端口
    uwsgi --http :9090 --wsgi-file helloworld.py
    
    • 添加并发性和监视调优

    uwsgi 默认是单进程单线程启动的

    添加多进程使用 --processes 选项, 添加多线程使用 --threads 选项

    uwsgi --http :9090 --wsgi-file helloworld.py --master --processes 4 --threads 2
    # 表示启动 4 个进程,每个进程 2 个线程;一个主进程,当进程挂掉后从新生成新的进程
    

    监测是一个重要的任务。在生产环境中知道发生了什么是至关重要的

    stats 子系统允许你以 json 格式导出 uwsgi 的内部统计信息

    uwsgi --http :9090 --wsgi-file helloworld.py --master --processes 4 --threads 2 --stats 127.0.0.1:9091
    # 添加监测,通过 127.0.0.1:9091 访问 json 格式的统计信息
    # 把 stats 绑定到一个私有地址,否则所有人都可以访问这个统计信息
    
    uwsgitop 一个类似 top 的监控 uwsgi 的工具
    pip install uwsgitop
    
    • 把 uwsgi 放到web服务器后端(nginx, apache等)

    uwsgi可以使用 HTTP, FastCGI, SCGI, uwsgi 协议,其中性能最好的是 uwsgi,nginx 支持 uwsgi 协议,apache 在添加对应的模块后也可以支持 uwsgi 协议

    一个常见的 nginx 配置如下:

    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:3031;
    }
    # 使用 uwsgi 协议将每个请求转发到绑定 3031 端口的服务器
    

    使用 uwsgi 协议启动 uwsgi 服务器

    uwsgi --socket 127.0.0.1:3031 --wsgi-file helloworld.py --master --processes 4 --threads 2 -stats 127.0.0.1:9191
    

    如果你的代理前端服务器使用的是 http 协议,你必须让 uwsgi 也使用 http 协议

    uwsgi --http-socket 127.0.0.1:3031 --wsgi-file helloworld.py --master --processes 4 --threads 2 --stats 127.0.0.1:9091
    
    # --http 和 --http-socket 不一样,--http 自带一个代理
    
    • 开机自动启动 uwsgi

    在使用脚本来启动 uwsgi 之前,看是否可以使用其他进程管理程序来启动 uwsgi,比如:

    Upstart, Systemd
    supervisord, god, monit, circus
    

    uwsgi 可以很好的与以上的进程管理器集成,如果有大量的应用需要部署,建议使用 uWSGI Emperor

    • 部署 django

    假设 django 项目路径是: /home/foobar/myproject

    uwsgi --socket 127.0.0.1:3031 --chdir /home/foobar/myproject/ --wsgi-file myproject/wsgi.py --master --processes 4 --theads 2 --stats 127.0.0.1:9191
    

    --chdir 切换到 django 的项目路径下,这是 django 正确加载模块所必须的

    以上这个很长的命令行命令可以写在一个 uwsgi 的 .ini 格式的配置文件中,比如 foo.ini

    [uwsgi]
    socket = 127.0.0.1:3031
    chdir = /home/foobar/myproject/
    wsgi-file = myproject/wsgi.py
    processes = 4
    threads = 2
    stats = 127.0.0.1:9191
    

    使用配置文件启动

    uwsgi foo.ini
    

    如果使用的是旧版本的django(< 1.4),需要多加一下配置

    uwsgi --socket 127.0.0.1:3031 --chdir /home/foobar/myproject/ --pythonpath .. --env DJANGO_SETTINGS_MODULE=myproject.settings --module "django.core.handlers.wsgi:WSGIHandler()" --processes 4 --threads 2 --stats 127.0.0.1:9191
    
    [uwsgi]
    socket = 127.0.0.1:3031
    chdir = /home/foobar/myproject/
    pythonpath = ..
    env = DJANGO_SETTINGS_MODULE=myproject.settings
    module = django.core.handlers.wsgi:WSGIHandler()
    processes = 4
    threads = 2
    stats = 127.0.0.1:9191
    

    旧版本的 django(< 1.4) 需要设置 env,module, pythonpath(允许获取到 myproject.settings 模块)

    • 部署 flask

    部署flask

    • 关于线程的说明

    如果没有使用多线程启动 uwsgi,那么在应用里面生成的线程不会运行

    可以使用 --enable-threads 选项,enable-threads = true .ini文件配置项,在不使用多线程启动 uwsgi 时支持应用内的多线程运行

    • python 虚拟环境

    uwsgi 支持 python 虚拟环境内查找 python 模块,只需要添加配置项 virtualenv = <path>

    • 安全和可用性

    安全性

    不要使用 root 用户运行 uwsgi,可以使用 uid 和 gid 移除特权

    [uwsgi]
    https = :9090,foobar.crt,fooar.key
    uid = foo
    gid = bar
    chdir = path_to_web2py
    module = wsgihandler
    master = true
    processes = 8
    

    如果要使用特权端口(如 443),可以使用共享 socket

    [uwsgi]
    shared-socket = :443
    https = 0, foobar.crt, foobar.key
    uid = foo
    gid = bar
    chdir = path_to_web2py
    module = wsgihandler
    master = true
    processes = 8
    

    可以性

    请求阻塞问题,可以设置一个 harakiri 定时器,在指定时间后销毁被阻塞的 worker

    [uwsgi]
    shared-socket = :443
    https = =0,foobar.crt,foobar.key
    uid = foo
    gid = bar
    chdir = path_to_web2py
    module = wsgihandler
    master = true
    processes = 8
    harakiri = 30
    

    如果一个请求阻塞了 30 秒,那么这个 worker 会被销毁

    可以通过 stats 子系统实时查看每个worker,thread 或者 async 工作情况

    • 线程卸载

    uwsgi offloading 子系统在特定匹配模式或可以使用纯C线程时,可以很快的释放掉 worker,比如:从文件系统发送静态文件,从网络发送数据等

    意思是:worker把请求中的特定操作(发送文件,发送数据等)分配给其他的进程线程,从而使得 worker 可以被尽快释放,处理跟多的请求

    添加 --offload-threads <n> 选项启用,(建议每个CPU启用 1 个线程)

    • 多 python 版本支持

    最好的方法是内置一个独立于语言的特性的二进制文件,并为每个Python版本提供一个按需加载的插件

    编译时使用 PROFILE=nolang

    make PROFILE=nolang
    

    编译python 插件

    PYTHON=python3.4 ./uwsgi --build-plugin "plugins/python python34"
    PYTHON=python2.7 ./uwsgi --build-plugin "plugins/python python27"
    PYTHON=python2.6 ./uwsgi --build-plugin "plugins/python python26"
    

    会得到3个插件:python34_plugin.so, python27_plugin.so, python26_plugin.so

    将这些复制到您想要的目录中,默认情况下,uWSGI在当前工作目录中搜索插件。

    现在可以在 uwsgi 配置文件中使用这些插件,添加在最顶端

    [uwsgi]
    plugins-dir = <path_to_your_plugin_directory>
    plugin = python26
    

    2019-2-20 by achxku@163.com

  • 相关阅读:
    Option使用和实现内幕源码揭秘之Scala学习笔记-22
    模式匹配高级实战:嵌套的Case class之Scala学习笔记-21
    Case class和Case object代码实战解析之Scala学习笔记-20
    Scala提取器Extractor实战详解之Scala学习笔记-19
    Type、Array、List、Tuple模式匹配实战解析之Scala学习笔记-18
    Scala中模式匹配入门实战详解之Scala学习笔记-17
    FragmentTabHost
    android项目中使用开源数据库litepal
    android Studio项目运行时报错“Could not identify launch activity: Default Activity not found”
    DrawerLayout学习,抽屉效果
  • 原文地址:https://www.cnblogs.com/xkus/p/10405710.html
Copyright © 2011-2022 走看看