# Python : Web : Gunicorn WSGI application server written in Python. ## Architecture The master process knows nothing about clients or requests, it just creates and maintains the worker processes, and listens for signals. #### Worker Types - sync - one request a time - no persistent connections - async (eventlet or gevent) - based on "Greenlets" aka cooperative multi-threading - shouldn't require code changes - if using psycopg, must install & setup psycogreen - gthread - threaded - accepts connections in main loop - supports keepalive connections > [!NOTE] Recommended Worker Count > 2 x cores + 1 > [!NOTE] Google Cloud Run Suggestion > `gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app` > https://cloud.google.com/run/docs/tips/python#optimize_gunicorn #### Packages If using a worker type other than `sync`, you must install the necessary packages: ``` greenlet # for both Eventlet or Gevent eventlet -or- gunicorn[ eventlet ] # for Eventlet workers gevent -or- gunicorn[ gevent ] # for Gevent workers gunicorn[ gthread ] # for threaded workers (CURRENTLY EMPTY) ``` ## CLI ``` gunicorn [OPTIONS] [APP_MODULE] -h|--help show this help message and exit -v|--version show program's version number and exit -c|--config CONFIG config file ( ./gunicorn.conf.py ) -b|--bind ADDRESS socket to bind ( 127.0.0.1:8000 ) --backlog INT max pending connections ( 2048 ) -w|--workers INT num worker procs ( 1 ) -k|--worker-class STRING worker type ( sync ) --threads INT num worker threads ( 1 ) --worker-connections INT max simultaneous clients ( 1000 ) --max-requests INT max requests before restarting ( 0 ) --max-requests-jitter INT max jitter to add to max_requests ( 0 ) -t|--timeout INT kill/restart worker after INT seconds of silence ( 30 ) --graceful-timeout INT timeout for graceful restart ( 30 ) --keep-alive INT seconds to wait for requests on a Keep-Alive connection ( 2 ) --limit-request-line INT max bytes of HTTP request line ( 4094 ) --limit-request-fields INT max HTTP headers ( 100 ) --limit-request-field_size INT max HTTP header field bytes ( 8190 ) --reload restart on code changes ( False ) --reload-engine STRING method used for reload ( auto ) (TODO: re-write and re-style the rest) --reload-extra-file FILES Extends :ref:`reload` option to also watch and reload on additional files [[]] --spew Install a trace function that spews every line executed by the server. [False] --check-config Check the configuration and exit. The exit status is 0 if the [False] --print-config Print the configuration settings as fully resolved. Implies :ref:`check-config`. [False] --preload Load application code before the worker processes are forked. [False] --no-sendfile Disables the use of ``sendfile()``. [None] --reuse-port Set the ``SO_REUSEPORT`` flag on the listening socket. [False] --chdir CHDIR Change directory to specified directory before loading apps. [/home/ofer/src/backend] -D|--daemon Daemonize the Gunicorn process. [False] -e|--env ENV Set environment variables in the execution environment. [[]] -p|--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 Switch worker processes to run as this user. [1000] -g|--group GROUP Switch worker process to run as this group. [1000] -m|--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] --disable-redirect-access-to-syslog Disable redirect access logs to syslog. [False] --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 The Error log file to write to. [-] --log-level LEVEL The granularity of Error log outputs. [info] --capture-output Redirect stdout/stderr to specified file in :ref:`errorlog`. [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-config-json FILE The log config to read config from a JSON file [None] --log-syslog-to SYSLOG_ADDR Address to send syslog messages. [udp://localhost:514] --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 The address of the StatsD server to log to. [None] --dogstatsd-tags DOGSTATSD_TAGS A comma-delimited list of datadog statsd (dogstatsd) tags to append to [] --statsd-prefix STATSD_PREFIX Prefix to use when emitting statsd metrics (a trailing ``.`` is added, [] -n|--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[r] 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). [2] --cert-reqs CERT_REQS Whether client certificate is required (see stdlib ssl module's) [0] --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 SSL Cipher suite to use, in the format of an OpenSSL cipher list. [None] --paste-global CONF Set a PasteDeploy global config variable in ``key=value`` form. [[]] --strip-header-spaces Strip spaces present between the header name and the the ``:``. [False] --permit-unconventional-http-method Permit HTTP methods not matching conventions, such as IANA registration guidelines [False] --permit-unconventional-http-version Permit HTTP version not matching conventions of 2023 [False] --casefold-http-method Transform received HTTP methods to uppercase [False] --header-map HEADER_MAP Configure how header field names are mapped into environ [drop] --tolerate-dangerous-framing Process requests with both Transfer-Encoding and Content-Length [False] ```