我正在使用 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 控制台中错误消息的图像 - 但似乎不是很有帮助: