【问题标题】:gunicron fail to start at specified portgunicorn 未能在特定端口启动
【发布时间】:2021-02-09 08:55:10
【问题描述】:

我试图让我的flask程序监听5000端口,这里是app/app.py的主要部分:

app = Flask(__name__)

@app.route('/')
def index():
    app.logger.info("Receiving a request")
    if (request.method == "POST"):
        query = request.args.get("query")
        file = request.files['file']
        app.logger.info(query)
        filePath  = '/tmp/tmpDoc'
        # write something to file
        searchResult = bootLoader.run(filePath, query)
        ans = searchResult['answers']
        return jsonify(ans)
    return "hihi"

if __name__ == "__main__":
    app.run(debug=True, port=5000)

还有我的wsgi.py

from app.app import app

if __name__ == "__main__":
    app.run(debug=True, port=5000)

我用 gunicorn wsgi:app 的 gunicorn 运行程序,但调试功能和指定的端口都不起作用。以下是日志:

[2021-02-09 16:50:58 +0800] [62555] [INFO] Starting gunicorn 20.0.4
[2021-02-09 16:50:58 +0800] [62555] [INFO] Listening at: http://127.0.0.1:8000 (62555)
[2021-02-09 16:50:58 +0800] [62555] [INFO] Using worker: sync
[2021-02-09 16:50:58 +0800] [62558] [INFO] Booting worker with pid: 62558

【问题讨论】:

  • 尝试使用bind。所以你的命令是gunicorn --bind 0.0.0.0:5000 wsgi:app
  • @neilharia7 是的,我必须以gunicorn --log-level=DEBUG -b 127.0.0.1:5000 wsgi:app 的身份运行程序。但是wsgi.py 中的app.run(...) 还不够配置吗?
  • 如果不指定绑定命令,gunicorn默认端口8000。根据文档, gunicorn 有自己的配置文件,它可以从中选择端口。您可以通过运行gunicorn --check-config 进行检查

标签: python flask gunicorn


【解决方案1】:

那是因为if __name__ == "__main__": 永远不会满足。

您可以运行python app/app.py 将使用内置的WSGI 开发服务器。 运行python wsgi.py 后也会发生同样的事情。

Gunicorn 命令未触发if __name__ == "__main__":。您可以将其视为 gunicorn 将导入 app 对象并自行运行它。这就是使用不同端口号的原因。看到如果你删除两个文件中的if __name__ == ...,guncorn 仍然可以在 8000 端口上运行服务器。

相反,您应该在 gunicorn 命令中使用 --bind 传递主机和端口号。

gunicorn wsgi:app -b :5000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 2012-10-04
    • 1970-01-01
    • 2015-04-09
    • 2010-10-31
    • 2015-08-15
    • 1970-01-01
    相关资源
    最近更新 更多