由于缺少必需的位置参数:“应用程序”,因此DispatcherMiddleware无法与gunicorn一起使用,但是如何指定?

问题描述

为了将不同的目标请求传递给特定的DashApp,我将dispatcherMiddleware集成到了Flask项目中。 它在本地运行良好,但是Heroku的gunicorn服务器缺少一个必需的位置参数:我称为我的应用程序的“应用程序”。 我需要如何执行位置参数才能使其运行?

  • 这是我通过http请求调用gunicorn后成功部署后的错误日志
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 "-" "-"
  • 这是我的run.py(在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
    )
web: gunicorn run:run_simple --log-file=-

解决方法

我发现了我的错误-必须在Procfile中调用该应用程序(而不是我之前尝试的方法)。 有了这个Procfile,它运行良好:

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