我开始学习谷歌应用程序引擎,并用Flask应用程序编写了一个基本的main.py文件,效果很好。以下是前几行代码:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/")
def root():
return jsonify({'status': "Success!"}), 200
我想更改脚本的名称,所以我将其重命名为"test-app.py",并将此行添加到app.yaml:
runtime: python38
entrypoint: test-app:app
然后重新运行gcloud应用程序部署。部署成功,但应用程序返回500,日志中显示:
2021-05-09 22:23:40 default[20210509t222122] "GET / HTTP/1.1" 500
2021-05-09 22:23:41 default[20210509t222122] /bin/sh: 1: exec: test-app:app: not found
我还尝试了文档中的这些:
entrypoint: gunicorn -b :$PORT test-app:app
entrypoint: uwsgi --http :$PORT --wsgi-file test-app.py --callable application
在这两种情况下,日志显示"/bin/sh:1:exec:(gunicorn|uwsgi(:找不到">
在Lambda中,入口点是通过handler选项设置的,默认情况下,handler选项是名为Lambda_function的文件中的一个名为Lambda _handler((的函数。似乎应用引擎使用";应用程序";内部";"main.py";,但是改变这个的正确语法是什么呢?
您的应用程序无法运行,很可能是因为您忘记将gunicorn添加到依赖项中
在requirements.txt
文件中添加以下行(您可以更改版本(:
gunicorn==19.3.0
然后在app.yaml
中添加以下行:
entrypoint: gunicorn -b :$PORT test_app:app
这应该足以让默认应用程序按预期运行
但是,如果您想为服务器进行更复杂的配置,您可以创建一个guniconrn.conf.py
并添加您的首选项。在这种情况下,您必须在您的入口点中指定它:
entrypoint: gunicorn -c gunicorn.conf.py -b :$PORT main:app
上面的答案基本上是正确的,但没有解释根本原因。文件称,gunicorn是";推荐的";web服务器,但实际上,当部署基于web的应用程序时,它是默认web服务器。
当在app Engine上部署Flask应用程序时,gunicorn会成为一个隐藏的依赖项,并假定main.py中的入口点为app((。对于自定义文件/入口点,它当然需要在YAML文件中设置,如下所示:
entrypoint: gunicorn -w 2 test_app:app
或者,如果使用uwsgi而不是gunicorn:作为web服务器
entrypoint: uwsgi --http :$PORT --wsgi-file test_app.py --callable app --enable-threads
无论哪种情况,uwsgi或gunicorn都不再是隐藏的依赖项,因此需要在requirements.txt文件中指定。就我个人而言,我对这两个都不熟悉,因为我使用过Werkzeug或Apache/mod_wsgi