Google App Engine:通过 Python 使用自定义入口点

Joh*_*yer 8 google-app-engine google-app-engine-python

我开始学习 Google App Engine,并使用 Flask 应用程序编写了一个基本的 main.py 文件,该文件运行良好。这是前几行代码:

from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/")
def root():
    return jsonify({'status': "Success!"}), 200
Run Code Online (Sandbox Code Playgroud)

我想更改脚本的名称,因此我将其重命名为“test-app.py”并将此行添加到 app.yaml 中:

runtime: python38
entrypoint: test-app:app
Run Code Online (Sandbox Code Playgroud)

然后重新运行 gcloud app 部署。部署成功,但应用程序返回 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
Run Code Online (Sandbox Code Playgroud)

我还从文档中尝试了这些:

entrypoint: gunicorn -b :$PORT test-app:app
entrypoint: uwsgi --http :$PORT --wsgi-file test-app.py --callable application
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,日志均显示“/bin/sh: 1: exec: (gunicorn|uwsgi): not found”

在 Lambda 中,入口点是通过处理程序选项设置的,默认情况下,该选项是名为 lambda_function 的文件中名为 lambda_handler() 的函数。看起来 App Engine 在“main.py”中使用“app”,但是更改此设置的正确语法是什么?

小智 7

您的应用程序无法运行很可能是因为您忘记将gunicorn 添加到依赖项中。
在您的requirements.txt文件中添加以下行(您可以更改版本):

gunicorn==19.3.0
Run Code Online (Sandbox Code Playgroud)

然后app.yaml添加以下行:

entrypoint: gunicorn -b :$PORT test_app:app
Run Code Online (Sandbox Code Playgroud)

这应该足以让默认应用程序按预期运行。
但是,如果您想要对服务器进行更复杂的配置,您可以创建一个guniconrn.conf.py并向其添加您的首选项。在这种情况下,您必须在入口点中指定它:

entrypoint: gunicorn -c gunicorn.conf.py -b :$PORT main:app
Run Code Online (Sandbox Code Playgroud)