【问题标题】:Google App Engine: Using custom entry point with PythonGoogle App Engine:在 Python 中使用自定义入口点
【发布时间】:2021-07-31 10:04:41
【问题描述】:

我开始学习 Google App Engine,并使用 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 app deploy。部署成功,但应用程序在日志中返回 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

我也从documentation尝试了这些:

entrypoint: gunicorn -b :$PORT test-app:app
entrypoint: uwsgi --http :$PORT --wsgi-file test-app.py --callable application

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

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

【问题讨论】:

  • 你为什么要改名字?你真的不应该。

标签: google-app-engine google-app-engine-python


【解决方案1】:

您的应用无法运行很可能是因为您忘记将 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

【讨论】:

    【解决方案2】:

    上面的这个答案大部分是正确的,但没有解释根本原因。文档说 gunicorn 是“推荐的”网络服务器,但实际上它是部署基于网络的应用程序时的默认网络服务器。

    在 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

    【讨论】:

      猜你喜欢
      • 2022-06-23
      • 2021-10-14
      • 2019-09-13
      • 1970-01-01
      • 2021-01-31
      • 2015-11-07
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      相关资源
      最近更新 更多