zoukankan      html  css  js  c++  java
  • 【转载】web 部署专题(一):Gunicorn运行与配置方法

     

     原文链接:https://www.cnblogs.com/qiu-hua/p/12680905.html

    Gunicorn“绿色独角兽”是一个被广泛使用的高性能的Python WSGI UNIX HTTP服务器,移植自Ruby的独角兽(Unicorn )项目,使用pre-fork worker模式,具有使用非常简单,轻量级的资源消耗,以及高性能等特点。

    安装gunicorn

    $ sudo apt-get update
    $ sudo apt-get install gunicorn

    运行gunicorn:

    $ gunicorn [OPTIONS] 模块名:变量名

    模块名是python文件名,可以是完整的路径+python文件名;变量名是python文件中可调用的WSGI(Web Server Gateway ).

    示例:

    复制代码
    # filename:test.py
    def app(environ, start_response):
    """Simplest possible application object"""
    data = 'Hello, World!\n'
    status = '200 OK'
    response_headers = [
    ('Content-type','text/plain'),
    ('Content-Length', str(len(data)))
    ]
    start_response(status, response_headers)
    return iter([data])
    复制代码

    运行app:

    $ gunicorn --workers=2 test:app

    常用配置参数:

    -c CONFIG, --config=CONFIG

    指定一个配置文件(py文件).

    -b BIND, --bind=BIND

    与指定socket进行绑定.

    -D, --daemon

    以守护进程形式来运行Gunicorn进程,其实就是将这个服务放到后台去运行。

    -w WORKERS, --workers=WORKERS

    工作进程的数量。上边提到gunicorn是一个pre-fork worker模式,就是指gunicorn启动的时候,在主进程中会预先fork出指定数量的worker进程在处理请求时,gunicorn依靠操作系统来提供负载均衡,通常推荐的worker数量是:(2 x $num_cores) + 1

    -k WORKERCLASS, --worker-class=WORKERCLASS

    工作进程类型. 包括 sync(默认), eventlet, gevent, or tornado, gthread, gaiohttp.

    --backlog INT

    最大挂起的连接数.

    --chdir

    切换到指定的工作目录.

    --log-level LEVEL

    输出error log的颗粒度,有效的LEVEL有:

    debug
    info
    warning
    error
    critical
    --access-logfile FILE

    确认要写入Access log的文件FILE. '-' 表示输出到标准输出.

    --error-logfile FILE, --log-file FILE

    确认要写入Error log的文件FILE. '-' 表示输出到标准错误输出.

    gunicorn配置

    Gunicorn从三个不同地方获取配置:

    框架设置(通常只影响到Paster应用)

    配置文件(python文件):配置文件中的配置会覆盖框架的设置。

    命令行

    框架设置只跟Paster(一个Web框架)有关,不讨论;命令行配置如上部分所示;现在我们看下怎么用配置文件配置gunicorn:

    配置文件必须是一个python文件,只是将命令行中的参数写进py文件中而已,如果需要设置哪个参数,则在py文件中为该参数赋值即可。例如:

    # example.py
    bind = "127.0.0.1:8000"
    workers = 2

    运行gunicorn:

    $ gunicorn -c example.py test:app

    等同于:

    $ gunicorn -w 2 -b 127.0.0.1:8000 test:app

    当然,配置文件还能实现更复杂的配置:

    复制代码
    # gunicorn.py
    import logging
    import logging.handlers
    from logging.handlers import WatchedFileHandler
    import os
    import multiprocessing
    bind = '127.0.0.1:8000'   #绑定ip和端口号
    backlog = 512        #监听队列
    chdir = '/home/test/server/bin' #gunicorn要切换到的目的工作目录
    timeout = 30   #超时
    worker_class = 'gevent' #使用gevent模式,还可以使用sync 模式,默认的是sync模式
    workers = multiprocessing.cpu_count() * 2 + 1  #进程数
    threads = 2 #指定每个进程开启的线程数
    loglevel = 'info' #日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置
    access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'  #设置gunicorn访问日志格式,错误日志无法设置
    """
    其每个选项的含义如下:
    h     remote address
    l     '-'
    u     currently '-', may be user name in future releases
    t     date of the request
    r     status line (e.g. ``GET / HTTP/1.1``)
    s     status
    b     response length or '-'
    f     referer
    a     user agent
    T     request time in seconds
    D     request time in microseconds
    L     request time in decimal seconds
    p     process ID
    """
    accesslog = "/home/test/server/log/gunicorn_access.log"   #访问日志文件
    errorlog = "/home/test/server/log/gunicorn_error.log"    #错误日志文件
    复制代码

    运行gunicorn:

    $ gunicorn -c gunicorn.py test:app

    本文来自博客园,作者:秋华,转载请注明原文链接:https://www.cnblogs.com/qiu-hua/p/12680905.html

  • 相关阅读:
    linux 下spyder安装
    【C++】fill函数,fill与memset函数的区别
    【tensorflow使用笔记一】:安装linux下tensorflow环境的问题
    leetcode 49 字母异位词分组
    leetcode 1014. 在 D 天内送达包裹的能力
    【C++进阶:STL常见性质3】
    【C++进阶:STL常见性质2】
    【C++进阶:STL常见性质】
    【C++进阶:移位运算符的用法】
    面向对象之静态方法
  • 原文地址:https://www.cnblogs.com/tortoise512/p/15615039.html
Copyright © 2011-2022 走看看