【问题标题】:How to run Flask app using gunicorn?如何使用 gunicorn 运行 Flask 应用程序?
【发布时间】:2015-03-25 19:42:36
【问题描述】:

问题可以分为两个独立的部分。

  1. 我有以下项目结构

    |- project
    |   |-- app/ - directory with actual project code
    |   |-- app.py - imports something from app/ and call create_app
    

    当我运行gunicorn 时,我应该将他指向app 对象,该对象实际上是在app.py 中创建的。所以我收到一个错误,因为 gunicorn 将 app:app 视为一个包。唯一的方法是重命名?

  2. 我使用工厂方法来创建应用程序。所以我在 app.py 中导入create_app 函数并将其从flask.ext.script 传递给Manager。我将经理对象传递给 gunicorn。在这种情况下, gunicorn 运行正常,但是一旦第一个请求到来,我就会收到以下错误:

    [2015-03-25 15:38:11 +0000] [14395] [ERROR] Error handling request
    Traceback (most recent call last):
      File "/opt/env/local/lib/python2.7/site-packages/gunicorn/workers/async.py", line 52, in handle
    self.handle_request(listener_name, req, client, addr)
      File "/opt/env/local/lib/python2.7/site-packages/gunicorn/workers/ggevent.py", line 159, in handle_request
    super(GeventWorker, self).handle_request(*args)
      File "/opt/env/local/lib/python2.7/site-packages/gunicorn/workers/async.py", line 105, in handle_request
        respiter = self.wsgi(environ, resp.start_response)
    TypeError: __call__() takes at most 2 arguments (3 given)
    

    也许我可以创建一些文件wsgi.py 并为 gunicorn 提供当前应用程序?

【问题讨论】:

    标签: python flask gunicorn


    【解决方案1】:

    如果两个名为“app”的东西位于 Python 路径中的同一目录中,您将遇到问题。

    您需要将 Flask 应用实例直接传递给 gunicorn。命令行管理器不是 WSGI 应用程序,这就是您收到该错误的原因。

    您可以简单地将 gunicorn 直接指向您的应用工厂的调用,无需任何中间代码。

    gunicorn app:create_app()
    

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 2022-07-07
      • 2012-02-15
      相关资源
      最近更新 更多