gpt4 book ai didi

python - 如何使用 gunicorn 运行 Flask 应用程序?

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:22 25 4
gpt4 key购买 nike

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

  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 提供当前应用程序?

最佳答案

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

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

您可以直接将 gunicorn 指向对应用工厂的调用,不需要任何中间代码。

gunicorn app:create_app()

关于python - 如何使用 gunicorn 运行 Flask 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29265044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com