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).
# __init__.py
# Normal entry point
def main():
...
# Gunicorn entry point generator
def app(*args, **kwargs):
# Gunicorn CLI args are useless.
# https://stackoverflow.com/questions/8495367/
#
# Start the application in modified environment.
# https://stackoverflow.com/questions/18668947/
#
import sys
sys.argv = ['--gunicorn']
for k in kwargs:
sys.argv.append("--" + k)
sys.argv.append(kwargs[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
.