gunicorn 无法在指定端口启动

gunicron fail to start at specified port

我试图让我的烧瓶程序监听端口 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

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

你可以运行python app/app.py什么都会用到内置的WSGI开发服务器。 运行宁python wsgi.py.

后也会发生同样的事情

Gunicorn 命令未触发 if __name__ == "__main__":。你可以把它想象成 gunicorn 会导入 app 对象并 运行 它自己。这就是使用不同端口号的原因。请注意,如果您删除两个文件中的 if __name__ == ...,guncorn 仍将能够 运行 端口 8000 上的服务器。

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

gunicorn wsgi:app -b :5000