• gunicorn


    参考:https://www.jianshu.com/p/fecf15ad0c9a

    https://www.cnblogs.com/nanrou/p/7026789.html 参数配置介绍

    https://gunicorn.readthedocs.io/en/latest/ 中文文档

    https://www.cnblogs.com/xybaby/tag/gunicorn/ 源码走读

    https://www.cnblogs.com/Ray-liang/p/4837850.html Flask + Gunicorn + Nginx 部署

    Tips. pip --user用法

    由于是部署在公司云主机上,通常不会给root权限。之前都是提工单给SA sudo装的,后来发现更安全也更方便的方法是pip install xxx --user,美中不足是安装完以后需要手动添加PATH
    export PATH=/home/username/.local/bin:$PATH
    方便起见可以加到~/.bash_profile

    =============

    主进程(master):主进程是一个简单的循环,监听各种进程的信号并做出相应的反应.它通过监听信号(比如TTIN/TTOU/CHLD)来管理正在运行的工作进程.TTIN和TTOU告诉主进程增加或减少工作的进程数量.CHLD表明一个子进程被终止了,这时主进程就重启这个失败的进程

    在应用启动之后,然后再通过TTIN和TTOU这两个信号来调整worker数量。

    ==============

    gunicorn -h

    usage: gunicorn [OPTIONS] [APP_MODULE]
    optional arguments:
      -h, --help            show this help message and exit
      -v, --version         show program's version number and exit
      --proxy-protocol      Enable detect PROXY protocol (PROXY mode). [False]
      --worker-connections INT
                            The maximum number of simultaneous clients. [1000]
      --statsd-host STATSD_ADDR
                            ``host:port`` of the statsd server to log to. [None]
      --max-requests-jitter INT
                            The maximum jitter to add to the *max_requests*
                            setting. [0]
      --pythonpath STRING   A comma-separated list of directories to add to the
                            Python path. [None]
      -R, --enable-stdio-inheritance
                            Enable stdio inheritance. [False]
      -k STRING, --worker-class STRING
                            The type of workers to use. [sync]
      --ssl-version SSL_VERSION
                            SSL version to use (see stdlib ssl module's) [3]
      --suppress-ragged-eofs
                            Suppress ragged EOFs (see stdlib ssl module's) [True]
      --log-syslog          Send *Gunicorn* logs to syslog. [False]
      --log-syslog-facility SYSLOG_FACILITY
                            Syslog facility name [user]
      --cert-reqs CERT_REQS
                            Whether client certificate is required (see stdlib ssl
                            module's) [0]
      --preload             Load application code before the worker processes are
                            forked. [False]
      --keep-alive INT      The number of seconds to wait for requests on a Keep-
                            Alive connection. [2]
      --access-logfile FILE
                            The Access log file to write to. [None]
      -g GROUP, --group GROUP
                            Switch worker process to run as this group. [0]
      --graceful-timeout INT
                            Timeout for graceful workers restart. [30]
      --do-handshake-on-connect
                            Whether to perform SSL handshake on socket connect
                            (see stdlib ssl module's) [False]
      --spew                Install a trace function that spews every line
                            executed by the server. [False]
      -w INT, --workers INT
                            The number of worker processes for handling requests.
                            [1]
      -n STRING, --name STRING
                            A base to use with setproctitle for process naming.
                            [None]
      --no-sendfile         Disables the use of ``sendfile()``. [None]
      -p FILE, --pid FILE   A filename to use for the PID file. [None]
      -m INT, --umask INT   A bit mask for the file mode on files written by
                            Gunicorn. [0]
      --worker-tmp-dir DIR  A directory to use for the worker heartbeat temporary
                            file. [None]
      --limit-request-fields INT
                            Limit the number of HTTP headers fields in a request.
                            [100]
      -c CONFIG, --config CONFIG
                            The Gunicorn config file. [None]
      --log-config FILE     The log config file to use. [None]
      --check-config        Check the configuration. [False]
      --statsd-prefix STATSD_PREFIX
                            Prefix to use when emitting statsd metrics (a trailing
                            ``.`` is added, []
      --proxy-allow-from PROXY_ALLOW_IPS
                            Front-end's IPs from which allowed accept proxy
                            requests (comma separate). [127.0.0.1]
      -u USER, --user USER  Switch worker processes to run as this user. [0]
      --forwarded-allow-ips STRING
                            Front-end's IPs from which allowed to handle set
                            secure headers. [127.0.0.1]
      --threads INT         The number of worker threads for handling requests.
                            [1]
      --max-requests INT    The maximum number of requests a worker will process
                            before restarting. [0]
      --limit-request-line INT
                            The maximum size of HTTP request line in bytes. [4094]
      --access-logformat STRING
                            The access log format. [%(h)s %(l)s %(u)s %(t)s
                            "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"]
      --certfile FILE       SSL certificate file [None]
      --chdir CHDIR         Chdir to specified directory before apps loading.
                            [/poc/poc]
      --paste STRING, --paster STRING
                            Load a PasteDeploy config file. The argument may
                            contain a ``#`` [None]
      --error-logfile FILE, --log-file FILE
                            The Error log file to write to. [-]
      --log-level LEVEL     The granularity of Error log outputs. [info]
      --capture-output      Redirect stdout/stderr to Error log. [False]
      --log-syslog-to SYSLOG_ADDR
                            Address to send syslog messages. [udp://localhost:514]
      --log-syslog-prefix SYSLOG_PREFIX
                            Makes Gunicorn use the parameter as program-name in
                            the syslog entries. [None]
      -D, --daemon          Daemonize the Gunicorn process. [False]
      --ciphers CIPHERS     Ciphers to use (see stdlib ssl module's) [TLSv1]
      -b ADDRESS, --bind ADDRESS
                            The socket to bind. [['127.0.0.1:8000']]
      -e ENV, --env ENV     Set environment variable (key=value). [[]]
      --reload              Restart workers when code changes. [False]
      --limit-request-field_size INT
                            Limit the allowed size of an HTTP request header
                            field. [8190]
      -t INT, --timeout INT
                            Workers silent for more than this many seconds are
                            killed and restarted. [30]
      --ca-certs FILE       CA certificates file [None]
      --settings STRING     The Python path to a Django settings module.
                            (deprecated) [None]
      --keyfile FILE        SSL key file [None]
      --backlog INT         The maximum number of pending connections. [2048]
      --logger-class STRING
                            The logger you want to use to log events in Gunicorn.
                            [gunicorn.glogging.Logger]

     示例:gunicorn -b 0.0.0.0:8080 -w 5  --log-file /var/log/poc/app.log --access-logfile /var/log/poc/app.acc --pid /var/log/poc/app.pid gf:app

    gunicorn -b 0.0.0.0:8080 -w 5 --threads=3 --worker-class=gthread --log-file /var/log/poc/app.log --access-logfile /var/log/poc/app.acc --pid /var/log/poc/app.pid gf:app

    /var/log/poc 这个目录要事先存在,否则报错

    =============

    # config.py
    import os
    import gevent.monkey
    gevent.monkey.patch_all()
    
    import multiprocessing
    
    # debug = True
    loglevel = 'debug'
    bind = "0.0.0.0:7001"
    pidfile = "log/gunicorn.pid"
    accesslog = "log/access.log"
    errorlog = "log/debug.log"
    daemon = True
    
    # 启动的进程数
    workers = multiprocessing.cpu_count()
    worker_class = 'gevent'
    x_forwarded_for_header = 'X-FORWARDED-FOR'

     ===========关于如何配置 Gunicorn 的实用建议=======

    转载:http://www.cocoachina.com/articles/27824

    Gunicorn 是一个 Python 的 WSGI HTTP 服务器。它所在的位置通常是在反向代理(如 Nginx)或者 负载均衡(如 AWS ELB)和一个 web 应用(比如 Django 或者 Flask)之间

    • 主进程的作用是确保 worker 数量与设置中定义的数量相同。因此如果任何一个 worker 挂掉,主线程都可以通过分发它自身而另行启动。
    • worker 的角色是处理 HTTP 请求。

    第一种并发方式(workers 模式,又名 UNIX 进程模式)

    每个 worker 都是一个加载 Python 应用程序的 UNIX 进程。worker 之间没有共享内存。

    建议的 workers 数量是 (2*CPU)+1

    对于一个双核(两个CPU)机器,5 就是建议的 worker 数量。

    gunicorn --workers=5 main:app

    第二种并发方式(多线程)

    Gunicorn 还允许每个 worker 拥有多个线程。在这种场景下,Python 应用程序每个 worker 都会加载一次,同一个 worker 生成的每个线程共享相同的内存空间。

    为了在 Gunicorn 中使用多线程。我们使用了 threads 模式。每一次我们使用 threads 模式,worker 的类就会是 gthread

    gunicorn --workers=5 --threads=2 --worker-class=gthread main:app

    在使用 worker 和多线程模式时建议的最大并发数量仍然是(2*CPU)+1

    因此如果我们使用四核(4 个 CPU)机器并且我们想使用 workers 和多线程模式,我们可以使用 3 个 worker 和 3 个线程来得到最大为 9 的并发请求数量。

     gunicorn --workers=3 --threads=3 --worker-class=gthread main:app

    第三种并发方式(“伪线程”)

    有一些 Python 库比如(gevent 和 Asyncio)可以在 Python 中启用多并发。那是基于协程实现的“伪线程”。

    Gunicrn 允许通过设置对应的 worker 类来使用这些异步 Python 库。这里的设置适用于我们想要在单核机器上运行的gevent

    gunicorn --worker-class=gevent --worker-connections=1000 --workers=3 main:app
    worker-connections 是对于 gevent worker 类的特殊设置。

    2*CPU)+1 仍然是建议的workers 数量。因为我们仅有一核,我们将会使用 3 个worker。

    在这种情况下,最大的并发请求数量是 3000。(3 个 worker * 1000 个连接/worker)

    通过调整Gunicorn设置,我们希望优化应用程序性能。

    1. 如果这个应用是 I/O 受限,通常可以通过使用“伪线程”(gevent 或 asyncio)来得到最佳性能。正如我们了解到的,Gunicorn 通过设置合适的 worker 类 并将 workers数量调整到 (2*CPU)+1 来支持这种编程范式。
    2. mport multiprocessing
      workers = multiprocessing.cpu_count() * 2 + 1
    3. 如果这个应用是 CPU 受限,那么应用程序处理多少并发请求就并不重要。唯一重要的是并行请求的数量。因为 Python’s GIL,线程和“伪线程”并不能以并行模式执行。实现并行性的唯一方法是增加**workers** 的数量到建议的 (2*CPU)+1,理解到最大的并行请求数量其实就是核心数。
    4. 如果不确定应用程序的内存占用,使用 多线程 以及相应的 gthread worker 类 会产生更好的性能,因为应用程序会在每个 worker 上都加载一次,并且在同一个 worker 上运行的每个线程都会共享一些内存,但这需要一些额外的 CPU 消耗。
    5. 如果你不知道你自己应该选择什么就从最简单的配置开始,就只是 workers 数量设置为 (2*CPU)+1 并且不用考虑 多线程。从这个点开始,就是所有测试和错误的基准环境。如果瓶颈在内存上,就开始引入多线程。如果瓶颈在 I/O 上,就考虑使用不同的 Python 编程范式。如果瓶颈在 CPU 上,就考虑添加更多内核并且调整 workers 数量
  • 相关阅读:
    第三方应用调用应用市场和唤醒APP
    tp5 输入域名即访问指定页面
    反射概述(静态 VS 动态语言)
    自定义注解
    元注解
    内置注解
    什么是注解
    线程小结(代码)
    线程池
    信号灯法
  • 原文地址:https://www.cnblogs.com/testzcy/p/11693214.html
Copyright © 2020-2023  润新知