Cloud Run/Gunicorn 一分钟后出现 502 错误

Cloud Run/Gunicorn giving 502 error after one minute

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

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 或云 运行,所以这不是似乎是问题的原因。我的 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 控制台中错误消息的图像 - 虽然似乎没有太大帮助:

我们遇到了类似的问题。可能您的云 运行 前面的 GCP 内部负载平衡器无法将请求传递给实例。这意味着某些进程使云 运行 实例在 60 秒后停止,因此它不会收到任何请求。根据 post, it might have something to do with cloud run interfering with the gunicorn workers. Since cloud run (managed) is a serverless environment, the order in which workers and code are loaded and shut down matters. You could try setting --preload and --timeout=0. Another article提示类似的事情。

解决了问题。我正在向 Firebase 托管域发送 HTTP POST 请求。 Firebase 托管域 POST 请求在 60 秒后超时(请参阅 )- 解决方案是直接调用云 运行 url。