Where is the Gunicorn config file?

PythonFlaskGunicorn

Python Problem Overview


The gunicorn documentation talks about editing the config files, but I have no idea where it is.

Probably a simple answer :) I'm on Amazon Linux AMI.

Python Solutions


Solution 1 - Python

The answer is in the documentation of gunicorn. http://docs.gunicorn.org/en/latest/configure.html

You can specify the config file with .ini or a python script.

For example, from the django-skel project

"""gunicorn WSGI server configuration."""
from multiprocessing import cpu_count
from os import environ


def max_workers():    
    return cpu_count()


bind = '0.0.0.0:' + environ.get('PORT', '8000')
max_requests = 1000
worker_class = 'gevent'
workers = max_workers()

And you can run the server using

gunicorn -c gunicorn.py.ini project.wsgi

Note that project.wsgi correspond to the location of your wsgi.

Solution 2 - Python

An example file is here: https://github.com/benoitc/gunicorn/blob/master/examples/example_config.py

You can just comment out what you don't need and then point Gunicorn at it like so:

gunicorn -c config.py myproject:app

Solution 3 - Python

As far as default names go, Gunicorn will look for a configuration file named gunicorn.conf.py in the directory where Gunicorn is executed

Solution 4 - Python

Default configs are read from site-packages/gunicorn/config.py

$ python -c "from distutils.sysconfig import get_python_lib; print('{}/gunicorn/config.py'.format(get_python_lib()))"
(output)
/somepath/flask/lib/python2.7/site-packages/gunicorn/config.py

You can run strace to see what files, in what order are being opened by gunicorn

gunicorn app:app -b 0.0.0.0:5000

$ strace gunicorn app:app -b 0.0.0.0:5000
stat("/somepath/flask/lib/python2.7/site-packages/gunicorn/config", 0x7ffd665ffa30) = -1 ENOENT (No such file or directory)
open("/somepath/flask/lib/python2.7/site-packages/gunicorn/config.so", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/somepath/flask/lib/python2.7/site-packages/gunicorn/configmodule.so", O_RDONLY) = -1 ENOENT (No
such file or directory)
open("/somepath/flask/lib/python2.7/site-packages/gunicorn/config.py", O_RDONLY) = 5
fstat(5, {st_mode=S_IFREG|0644, st_size=53420, ...}) = 0
open("/somepath/flask/lib/python2.7/site-packages/gunicorn/config.pyc", O_RDONLY) = 6

gunicorn -c g_config.py app:app

$ strace gunicorn -c g_config.py app:app
//    in addition to reading default configs, reads '-c' specified config file
stat("g_config.py", {st_mode=S_IFREG|0644, st_size=6784, ...}) = 0
open("g_config.py", O_RDONLY)

Instead of modifying the config file in site-packages, create a local gconfig.py (any name) and define only the variables you want to set non-default value since default config file is always read.

Pass as gunicorn -c gconfig.py

$ cat gconfig.py // (eg)
bind = '127.0.0.1:8000'
workers = 1
timeout = 60
. . .

or use command line options instead of a config file:

gunicorn app:app -b 0.0.0.0:5000 -w 1 -t 60

gunicorn config file attributes/flags: http://docs.gunicorn.org/en/stable/settings.html#settings

Solution 5 - Python

I did this after reading the docs:


  1. when deploying my app through gunicorn, usually there is a file called Procfile
  2. open this file, add --timeout 600

finally, my Procfile would be like:

web: gunicorn app:app --timeout 600

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkennysongView Question on Stackoverflow
Solution 1 - PythonArnaud JolyView Answer on Stackoverflow
Solution 2 - PythoniamyojimboView Answer on Stackoverflow
Solution 3 - PythonJoeView Answer on Stackoverflow
Solution 4 - PythonbrokenfootView Answer on Stackoverflow
Solution 5 - PythonBùi Việt Thiện ThưView Answer on Stackoverflow