Search code examples
pythonflaskherokugunicornwerkzeug

DispatcherMiddleware doesn't work with gunicorn because of a missing required positional argument: 'application', but how to specify?


To pass different target requests to their specific DashApps I've integrated a DispatcherMiddleware into my Flask project. Locally it's running fine, but gunicorn server at Heroku is missing one required positional argument: 'application' as I called my app. How do I need to do the positional argument to get it running?

  • This is my error log after the successful deploy after calling gunicorn via a http request
2020-11-04T08:14:23.211801+00:00 heroku[web.1]: State changed from starting to up
2020-11-04T08:15:55.608341+00:00 app[web.1]: [2020-11-04 08:15:55 +0000] [10] [ERROR] Error handling request /
2020-11-04T08:15:55.608354+00:00 app[web.1]: Traceback (most recent call last):
2020-11-04T08:15:55.608355+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 134, in handle
2020-11-04T08:15:55.608355+00:00 app[web.1]:     self.handle_request(listener, req, client, addr)
2020-11-04T08:15:55.608356+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 175, in handle_request
2020-11-04T08:15:55.608356+00:00 app[web.1]:     respiter = self.wsgi(environ, resp.start_response)
2020-11-04T08:15:55.608424+00:00 app[web.1]: TypeError: run_simple() missing 1 required positional argument: 'application'
2020-11-04T08:15:55.611228+00:00 app[web.1]: 10.63.193.41 - - [04/Nov/2020:08:15:55 +0000] "GET / HTTP/1.1" 500 0 "-" "-"
  • This is my run.py (which contains the argument application below run_simple)
from werkzeug.middleware.dispatcher import DispatcherMiddleware
from werkzeug.serving import run_simple


from app_datacenter.flask_app import flask_app


from app_datacenter.dash_apps.sessionlist_dummydata import app as sessionlist
from app_datacenter.dash_apps.sessionreport_realdata import app as sessionreport




application = DispatcherMiddleware(flask_app,
    {
    '/app1': sessionlist.server,
    '/app2': sessionreport.server
    }
)




if __name__ == '__main__':
    run_simple(
        hostname='localhost',
        port=5000,
        application=application,
        use_reloader=True,
        use_debugger=True,
        use_evalex=True
    )
  • This is my Procfile
web: gunicorn run:run_simple --log-file=-

Solution

  • I found my error - the application must be called inside the Procfile (and not the method as I tried before). With this Procfile it's running fine:

    web: gunicorn run:application --log-file=-