Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gunicorn can't find app when name changed from "application"

I use gunicorn --workers 3 wsgi to run my Flask app. If I change the variable application to myapp, Gunicorn gives the error AppImportError: Failed to find application: 'wsgi'. Why am I getting this error and how do I fix it?

myproject.py:

from flask import Flask

myapp = Flask(__name__)

@myapp.route("/")
def hello():
    return 'Test!'

if __name__ == "__main__":
    myapp.run(host='0.0.0.0')

wsgi.py:

from myproject import myapp

if __name__ == "__main__":
    myapp.run()
like image 395
xiº Avatar asked Oct 27 '15 22:10

xiº


People also ask

What does Gunicorn App App mean?

Gunicorn is a pure-Python HTTP server for WSGI applications. It allows you to run any Python application concurrently by running multiple Python processes within a single dyno. It provides a perfect balance of performance, flexibility, and configuration simplicity.

Where are Gunicorn logs?

Gunicorn Logs This log file is located at /var/log/cloudify/rest/gunicorn-access. log .

How do I know if Gunicorn is working?

You should receive the HTML output from your application in the terminal. This confirms that Gunicorn was started and able to serve your Django application. You can verify that the Gunicorn service is running by checking the status again: sudo systemctl status gunicorn.

What does Gunicorn -- Bind do?

--bind : This tells Gunicorn the IP and the port to listen to, using a colon ( : ) to separate the IP and the port. If you were running Uvicorn directly, instead of --bind 0.0. 0.0:80 (the Gunicorn option) you would use --host 0.0.


2 Answers

Gunicorn (and most WSGI servers) defaults to looking for the callable named application in whatever module you point it at. Adding an alias from myproject import myapp as application or application = myapp will let Gunicorn discover the callable again.

However, the wsgi.py file or the alias aren't needed, Gunicorn can be pointed directly at the real module and callable.

gunicorn myproject:myapp --workers 16
# equivalent to "from myproject import myapp as application"

Gunicorn can also call an app factory, optionally with arguments, to get the application object. (This briefly did not work in Gunicorn 20, but was added back in 20.0.1.)

gunicorn 'myproject.app:create_app("production")' --workers 16
# equivalent to:
# from myproject.app import create_app
# application = create_app("production")

For WSGI servers that don't support calling a factory, or for other more complicated imports, a wsgi.py file is needed to do the setup.

from myproject.app import create_app
app = create_app("production")
gunicorn wsgi:app --workers 16
like image 176
davidism Avatar answered Oct 24 '22 06:10

davidism


If you're trying to serve an app with variable name app within server/cats.py, you can start the server on port 8000 as follows:

gunicorn server.cats:app -b 0.0.0.0:8000
like image 22
duhaime Avatar answered Oct 24 '22 07:10

duhaime