Gunicorn can't find app when name changed from “application”

前端 未结 2 1268
天涯浪人
天涯浪人 2020-12-01 06:12

I use gunicorn --workers 3 wsgi to run my Flask app. If I change the variable application to myapp, Gunicorn gives the error Ap

相关标签:
2条回答
  • 2020-12-01 06:29

    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
    
    0 讨论(0)
  • 2020-12-01 06:53

    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
    
    0 讨论(0)
提交回复
热议问题