0

我正在使用 Gunicorn 的 Google Cloud Run 中部署一个 python 应用程序。我的 gunicorn 和 cloud run 超时都设置为 900 秒,这也是 Cloud Run 的超时。奇怪的是,当我调用该函数时,如果应用程序运行时间超过 60 秒,我会从 Cloud Run 收到 502 错误,如果运行时间少于 60 秒,则不会。例如,下面的部署函数抛出了这个错误:

def process_file(request=request):
    time.sleep(61)
    ...
    return handle_response()      

但是,如果我将睡眠时间更改为 40 秒:

def process_file(request=request):
    time.sleep(40)
    ...
    return handle_response() 

没有502错误。起初我以为问题是由 nginx 引起的,它有 60 秒的默认超时时间,但似乎 nginx 不是默认使用 docker 或 cloud run 部署的,所以这似乎不是问题的原因。我的 Dockerfile 如下:

FROM continuumio/miniconda3

# Install production dependencies.
RUN conda install numpy==1.17.2
RUN conda install xlsxwriter==1.1.2
RUN conda install pandas==0.25.1
RUN conda install -c conda-forge ciso8601
RUN pip install gunicorn flask gevent flask_mail flask-cors pyjwt firebase_admin networkx datefinder google-cloud-pubsub 

# Copy local code to the container image.
COPY app.py .
RUN mkdir backend/
COPY backend/ /backend/

# 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 app:app --timeout 900 --log-level debug

axios在前端调用云运行,据我了解,它没有超时,所以我不认为这应该是一个问题。任何帮助表示赞赏,谢谢!

编辑:这是 chrome 控制台中错误消息的图像 - 但似乎不是很有帮助:

在此处输入图像描述

4

2 回答 2

0

我们遇到了类似的问题。可能您的云运行前的 GCP 内部负载均衡器无法将请求传递给实例。这意味着某些进程使云运行实例在 60 秒后停止,因此它没有收到任何请求。根据这篇文章,这可能与云运行干扰 gunicorn 工人有关。由于云运行(托管)是无服务器环境,因此加载和关闭工作程序和代码的顺序很重要。您可以尝试设置--preload--timeout=0. 另一篇文章提出了类似的建议。

于 2020-11-22T11:37:31.963 回答
0

想通了这个问题。我正在向 Firebase 托管域发送 HTTP POST 请求。Firebase 托管域 POST 请求在 60 秒后超时(请参阅Firebase-Hosted Cloud Function retrying on any request that take 60s, even when timeout is >60s) - 解决方案是直接调用 Cloud Run url。

于 2020-12-08T06:57:35.030 回答