Using additional command line arguments with gunicorn


Assuming I’m starting a Flask app under gunicorn as per, is there a way for me to include/parse/access additional command line arguments?

E.g., can I include and parse the foo option in my Flask application somehow?

gunicorn mypackage:app --foo=bar


Asked By: sholsapp



You can’t pass command line arguments directly but you can choose application configurations easily enough.

$ gunicorn 'mypackage:build_app(foo="bar")'

Will call the function “build_app” passing the foo=”bar” kwarg as expected. This function should then return the WSGI callable that’ll be used.

Answered By: Paul J. Davis

I usually put it in __init.py__ after main() and then I can run with or without gunicorn (assuming your main() supports other functions too).


# Normal entry point
def main():

# Gunicorn entry point generator
def app(*args, **kwargs):
    # Gunicorn CLI args are useless.
    # Start the application in modified environment.
    import sys
    sys.argv = ['--gunicorn']
    for k in kwargs:
        sys.argv.append("--" + k)
    return main()

That way you can run simply with e.g.

gunicorn 'app(foo=bar)' ...

and your main() can use standard code that expects the arguments in sys.argv.

Answered By: personal_cloud
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.