From gunicorn¶
The options of gunicorn in the pre-forking server.
This document translates the options, the worker model and the habits of a
deployment of gunicorn into the ones of Pyjoyment.Command.prefork,
the command of Pyjo.Server.Prefork: for an application of
Pyjoyment that gunicorn has served through WSGI, as
PYJO_WSGI=1 gunicorn app:application, which runs on its own server
instead, and for a WSGI application of another framework, as Flask, that
a script of Pyjoyment.Lite mounts, as Mounting describes it,
and Django for a project of Django.
Options¶
Each option of Pyjoyment.Command.prefork that is not given has the
value of the environment variable of PYJO_ and its name, as
PYJO_WORKERS of -w, so GUNICORN_CMD_ARGS is one variable for each
option.
gunicorn |
Pyjoyment |
|---|---|
|
|
|
|
|
|
|
|
the sync workers, one request at a time |
|
|
|
|
the |
|
|
|
|
none |
|
|
|
|
|
|
none: up to half of |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
none: the heartbeats go through a pipe |
|
always: the application is loaded before the workers are forked |
|
|
|
none: the user of the container, as |
Workers¶
A worker of gunicorn of the default class serves one request at a time, and
closes the connection after it. -c 1 alone serves one connection at a
time, but keeps it alive, so an idle connection of a client or a proxy holds
the worker for the keep-alive timeout, while the other connections wait in
the backlog. Without -c 1, a slow request of an application that blocks
holds the other connections of its worker, while other workers are idle.
-r 1 closes the connection after each response, as gunicorn does, which
loses nothing behind a proxy, and avoids the race of a proxy that reuses a
connection that the server closes.
$ python app.py prefork -w 8 -c 1 -r 1
An application that does not block, as one of Pyjoyment that awaits
its I/O, keeps the defaults, with many connections in each worker.
Restarts of workers¶
--max-requests-jitter adds a random number up to its value to
--max-requests of each worker of gunicorn, so that the workers, which
share the load evenly, do not restart at the same moment. -a subtracts a
random number up to half of its value instead, so -a 1000 replaces a
worker after 500 to 1000 connections, 750 on average, and
--max-requests 1000 --max-requests-jitter 100, 1050 on average, is
-a 1400, 700 to 1400. -s, 2 by default, starts the new worker while
the old one finishes, forked from the manager, which has the application
loaded, so a restart costs little.
Containers¶
The manager is the process that gets TERM, so a shell in the command of
a container runs it with exec, which makes it PID 1:
CMD ["sh", "-c", "exec python app.py prefork -w ${WORKERS:-4} -c 1 -r 1"]
Kubernetes kills a container terminationGracePeriodSeconds after
TERM, 30 seconds by default, so the shutdown delay and the graceful
timeout go into it: generate kubernetes sets it to their sum and 5
seconds more, and a deployment that keeps 30 seconds shortens the graceful
timeout, as PYJO_SHUTDOWN_DELAY=5 with PYJO_GRACEFUL_TIMEOUT=20.
prefork writes only its pid file, prefork.pid in the temporary
directory, so a read-only root filesystem needs a writable TMPDIR, as an
emptyDir on /tmp, or -P in another directory.
You can continue with Guides now.