python - 从 GCP 安装文档运行 "[CRITICAL] WORKER TIMEOUT"时,日志中的 "Hello Cloud Run with Python"

标签 python flask google-cloud-platform gunicorn google-cloud-run

关注 tutorial here我有以下2个文件:
应用程序

from flask import Flask, request

app = Flask(__name__)


@app.route('/', methods=['GET'])
def hello():
    """Return a friendly HTTP greeting."""
    who = request.args.get('who', 'World')
    return f'Hello {who}!\n'


if __name__ == '__main__':
    # Used when running locally only. When deploying to Cloud Run,
    # a webserver process such as Gunicorn will serve the app.
    app.run(host='localhost', port=8080, debug=True)
文件
# Use an official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.7-slim

# Install production dependencies.
RUN pip install Flask gunicorn

# Copy local code to the container image.
WORKDIR /app
COPY . .

# Service must listen to $PORT environment variable.
# This default value facilitates local development.
ENV PORT 8080

# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn --bind 0.0.0.0:$PORT --workers 1 --threads 8 app:app
然后我使用 Cloud Build 和 Cloud Run 构建并运行它们:
PROJECT_ID=$(gcloud config get-value project)
DOCKER_IMG="gcr.io/$PROJECT_ID/helloworld-python"
gcloud builds submit --tag $DOCKER_IMG
gcloud run deploy --image $DOCKER_IMG --platform managed
代码似乎运行良好,我可以通过给定的 URL 访问该应用程序。然而,日志似乎表明一个严重错误,并且工作人员不断重新启动。这是启动应用程序并在我的网络浏览器中发出一些请求后来自 Cloud Run 的日志文件:
2020-03-05T03:37:39.392Z Cloud Run CreateService helloworld-python ...
2020-03-05T03:38:03.285477Z[2020-03-05 03:38:03 +0000] [1] [INFO] Starting gunicorn 20.0.4
2020-03-05T03:38:03.287294Z[2020-03-05 03:38:03 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
2020-03-05T03:38:03.287362Z[2020-03-05 03:38:03 +0000] [1] [INFO] Using worker: threads
2020-03-05T03:38:03.318392Z[2020-03-05 03:38:03 +0000] [4] [INFO] Booting worker with pid: 4
2020-03-05T03:38:15.057898Z[2020-03-05 03:38:15 +0000] [1] [INFO] Starting gunicorn 20.0.4
2020-03-05T03:38:15.059571Z[2020-03-05 03:38:15 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
2020-03-05T03:38:15.059609Z[2020-03-05 03:38:15 +0000] [1] [INFO] Using worker: threads
2020-03-05T03:38:15.099443Z[2020-03-05 03:38:15 +0000] [4] [INFO] Booting worker with pid: 4
2020-03-05T03:38:16.320286ZGET200 297 B 2.9 s Safari 13  https://helloworld-python-xhd7w5igiq-ue.a.run.app/
2020-03-05T03:38:16.489044ZGET404 508 B 6 ms Safari 13  https://helloworld-python-xhd7w5igiq-ue.a.run.app/favicon.ico
2020-03-05T03:38:21.575528ZGET200 288 B 6 ms Safari 13  https://helloworld-python-xhd7w5igiq-ue.a.run.app/
2020-03-05T03:38:27.000761ZGET200 285 B 5 ms Safari 13  https://helloworld-python-xhd7w5igiq-ue.a.run.app/?who=me
2020-03-05T03:38:27.347258ZGET404 508 B 13 ms Safari 13  https://helloworld-python-xhd7w5igiq-ue.a.run.app/favicon.ico
2020-03-05T03:38:34.802266Z[2020-03-05 03:38:34 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:4)
2020-03-05T03:38:35.302340Z[2020-03-05 03:38:35 +0000] [4] [INFO] Worker exiting (pid: 4)
2020-03-05T03:38:48.803505Z[2020-03-05 03:38:48 +0000] [5] [INFO] Booting worker with pid: 5
2020-03-05T03:39:10.202062Z[2020-03-05 03:39:09 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:5)
2020-03-05T03:39:10.702339Z[2020-03-05 03:39:10 +0000] [5] [INFO] Worker exiting (pid: 5)
2020-03-05T03:39:18.801194Z[2020-03-05 03:39:18 +0000] [6] [INFO] Booting worker with pid: 6
请注意工作人员超时并在日志末尾重新启动。它是一个严重错误这一事实让我认为它不应该发生。这是预期的行为吗?这是 Cloud Run 机器在请求来来去去时启动和停止我的服务的副作用吗?

最佳答案

Cloud Run 已缩减您的一个实例,并且 gunicorn仲裁者正在考虑它停滞不前。

您应该添加 --timeout 0给您的 gunicorn调用以完全禁用工作器超时,对于 Cloud Run 来说是不必要的。

关于python - 从 GCP 安装文档运行 "[CRITICAL] WORKER TIMEOUT"时,日志中的 "Hello Cloud Run with Python",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60537977/

相关文章:

Python session SAMESITE=None 未设置

java - 如何从数据存储 Google Cloud 检索文本类型?

python - 这是静态绑定(bind)吗?

python - 在 Python 中加载 JSON 文件抛出错误。

python - 如何将 nosetests 的输出重定向到文本文件?

x Axis 上的 Python 条形字符串

Python Flask - 将 POST 数据添加到 sqlite 数据库

python - 如何在 flask 中生成一个模板而不是另一个模板?

postgresql - 如何转储 Google Cloud SQL for PostgreSQL DB 以导入回常规 PostgreSQL 数据库?

python - 如何在等待外部服务器的post请求响应时绕过30秒的TTL