Commands

After installing Gunicorn you will have access to the command line script gunicorn.

gunicorn

Basic usage:

  1. $ gunicorn [OPTIONS] APP_MODULE

Where APP_MODULE is of the pattern $(MODULE_NAME):$(VARIABLE_NAME). The module name can be a full dotted path. The variable name refers to a WSGI callable that should be found in the specified module.

Example with the test app:

  1. def app(environ, start_response):
  2. """Simplest possible application object"""
  3. data = b'Hello, World!\n'
  4. status = '200 OK'
  5. response_headers = [
  6. ('Content-type', 'text/plain'),
  7. ('Content-Length', str(len(data)))
  8. ]
  9. start_response(status, response_headers)
  10. return iter([data])

You can now run the app with the following command:

  1. .. code-block:: text

$ gunicorn –workers=2 test:app

The variable name can also be a function call. In that case the name will be imported from the module, then called to get the application object. This is commonly referred to as the “application factory” pattern.

  1. def create_app():
  2. app = FrameworkApp()
  3. ...
  4. return app
  1. $ gunicorn --workers=2 'test:create_app()'

Positional and keyword arguments can also be passed, but it is recommended to load configuration from environment variables rather than the command line.

Commonly Used Arguments

  • -c CONFIG, --config=CONFIG - Specify a config file in the form $(PATH), file:$(PATH), or python:$(MODULE_NAME).
  • -b BIND, --bind=BIND - Specify a server socket to bind. Server sockets can be any of $(HOST), $(HOST):$(PORT), fd://$(FD), or unix:$(PATH). An IP is a valid $(HOST).
  • -w WORKERS, --workers=WORKERS - The number of worker processes. This number should generally be between 2-4 workers per core in the server. Check the FAQ for ideas on tuning this parameter.
  • -k WORKERCLASS, --worker-class=WORKERCLASS - The type of worker process to run. You’ll definitely want to read the production page for the implications of this parameter. You can set this to $(NAME) where $(NAME) is one of sync, eventlet, gevent, tornado, gthread. sync is the default. See the worker_class documentation for more information.
  • -n APP_NAME, --name=APP_NAME - If setproctitle is installed you can adjust the name of Gunicorn process as they appear in the process system table (which affects tools like ps and top).

Settings can be specified by using environment variable GUNICORN_CMD_ARGS.

See Configuration Overview and Settings for detailed usage.