最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】部署Flask的gunicorn的最后一个参数的含义

Flask crifan 3258浏览 0评论

折腾:

【已解决】Mac本地用gunicorn部署启动Flask应用

期间,参考:

Deployment Options — Flask Documentation (0.12)

独立 WSGI 容器 — Flask 0.10.1 文档

对于示例:

<code>gunicorn -w 4 -b 127.0.0.1:4000 myproject:app
</code>

中的最后的myproject:app是什么意思,需要去搞懂

通过之前看到的help中:

<code>➜  robotDemo gunicorn --help
usage: gunicorn [OPTIONS] [APP_MODULE]

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

感觉属于:

usage: gunicorn [OPTIONS] [APP_MODULE]

中的APP_MODULE

估计是:

python文件名中的flask的application名

而此处:

<code>➜  robotDemo pwd
/Users/crifan/dev/dev_root/company/naturling/projects/robotDemo
➜  robotDemo ll
total 48
-rw-r--r--  1 crifan  staff   212B  4 20 10:15 Pipfile
-rw-r--r--  1 crifan  staff   7.8K  4 20 10:16 Pipfile.lock
-rw-r--r--  1 crifan  staff   8.3K  4 19 21:40 app.py
drwxr-xr-x  3 crifan  staff    96B  4 18 17:43 logs
</code>

python文件是:

app.py,所以是:

app:xxx

再去找xxx:

此处的Flask的实例的变量名是app

所以,此处是

app:app

参考:

https://www.cnblogs.com/Ray-liang/p/4837850.html

更加确定我的猜测。

gunicorn 参数解释

Gunicorn运行与配置 – 简书

“运行gunicorn:

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

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

【总结】

gunicorn的示例:

<code>gunicorn -w 4 -b 127.0.0.1:4000 myproject:app
</code>

中的:

myproject:app

的含义是:

是属于

<code>gunicorn --help
usage: gunicorn [OPTIONS] [APP_MODULE]
</code>

中的APP_MODULE

APP_MODULE的格式是:

python文件名:WSGI的应用的变量名

(此处)==python文件名:Flask的实例的变量名

比如对于:

yourPythonName.py

中有:

<code>flaskInstanceVariable = Flask(__name__)
</code>

if __name__ == “__main__”:

    flaskInstanceVariable.run()

的话,则是:

yourPythonName:flaskInstanceVariable

【后记】

Gunicorn快速入门 – Arts&Crafts – 博客园

“APP_MODULE 指定 wsgi application文件,书写格式 $(MODULE_NAME):$(VARIABLE_NAME)。其中 module_name用来制定将要运行的 wsgi application文件,可是一个完整的点缀名。比如当前目录 myapp 目录下有个 Python 包  gunicorn_app, gunicorn_app包下有一个wsgi application文件 test.py 则 module_name可以直接写成  gunicorn_app.test。viriable_name表示在 module_name 文件中要调用的对象(是一个WSGI callable, 可以是一个函数,类详情参看WSGI规格说明书)名。”

WSGI规格说明书 – Arts&Crafts – 博客园

转载请注明:在路上 » 【已解决】部署Flask的gunicorn的最后一个参数的含义

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.139 seconds, using 22.19MB memory