大佬教程收集整理的这篇文章主要介绍了Google Cloud Run 上的 Gunicorn 获得 504 错误状态(上游请求超时),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我的代码在启动时需要加载机器学习模型,该模型获得 1 个大约 100MB 的 pickle,在我的情况下,我需要加载 6 个 pickle 文件(大约 600mb++),并且我使用 FastAPI 作为我的 API 代码与 Uvicorn和 Gunicorn。
所以,我想问的是,为什么我的gunicorn第一次从Cloud Run打开我的URL大约15秒时总是得到504错误状态码,之后就可以正常打开URL了。但是我离开它没有打开网址大约30-60分钟后,它会再次返回504错误吗?我的 Gunicorn 死了/关机了吗?因为当我从 Cloud Run 日志中查看它时,我的 gunicorn 得到了 ShutTing down
,我认为我的 gunicorn 已经死了。所以我需要让我的 Gunicorn 始终开启,但我怎样才能让我的 Gunicorn 始终开启?
这就是我的泡菜加载方式:
# Load all model
@app.on_event("startup")
async def load_model():
# Pathfile
pathfile_model = os.path.join("modules","model/")
pathfile_data = os.path.join("modules","data/")
start_time = time.time()
# Load Model
usedcar.price_ENGIne_4w = {}
top5_brand = ["honda","toyota","nissan","suzuki","daihatsu"]
for i in top5_brand:
with open(pathfile_model + f'{i}_all_in_one.pkl','rb') as file:
usedcar.price_ENGIne_4w[i] = pickle.load(filE)
with open(pathfile_model + 'ex_top5_all_in_one.pkl','rb') as file:
usedcar.price_ENGIne_4w['non'] = pickle.load(filE)
# Load Dataset Match
with open(pathfile_data + setTings.DATA_List) as path:
usedcar.List_match_seva = pd.read_csv(path)
elapsed_time = time.time() - start_time
print("======================================")
print("INFO : Model loaded succesfully")
print("MODEL :",usedcar.price_ENGIne_4w)
print("ELAPSED MODEL TIME : ",elapsed_timE)
这是我的 main.py 代码的运行方式:
if __name__ == "__main__":
uvicorn.run(app,host="0.0.0.0",port=8080,log_level="info",loop=asyncio)
这是我的 Dockerfile :
FROM python:3.8-slim-buster
RUN apt-get update --fix-missing
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y libgl1-mesa-dev python3-pip git
RUN mkdir /usr/src/app
workdir /usr/src/app
copY ./requirements.txt /usr/src/app/requirements.txt
RUN pip3 install -U setuptools
RUN pip3 install --upgrade pi
RUN pip3 install -r ./requirements.txt --use-feature=2020-resolver
copY . /usr/src/app
CMD exec gunicorn --bind :8080 --workers 2 --threads 4 main:app --worker-class uvicorn.workerS.UvicornH11Worker --preload --timeout 60 --worker-tmp-dir /dev/shm
这是我对 uvicorn 和 gunicorn 的要求:
fastAPI
fastAPI-utils
uvicorn[standard]
gunicorn
这是我的 Cloud Run 日志:
2021-02-15 14:31:54.346 WIT [2021-02-15 07:31:54 +0000] [1] [INFO] Handling signal: term
2021-02-15 14:31:54.385 WIT [2021-02-15 07:31:54 +0000] [11] [INFO] ShutTing down
2021-02-15 14:31:54.386 WIT [2021-02-15 07:31:54 +0000] [12] [INFO] ShutTing down
2021-02-15 14:31:54.486 WIT [2021-02-15 07:31:54 +0000] [11] [INFO] WaiTing for application shutdown.
2021-02-15 14:31:54.486 WIT [2021-02-15 07:31:54 +0000] [11] [INFO] Application shutdown complete.
2021-02-15 14:31:54.486 WIT [2021-02-15 07:31:54 +0000] [12] [INFO] WaiTing for application shutdown.
2021-02-15 14:31:54.486 WIT [2021-02-15 07:31:54 +0000] [11] [INFO] Finished server process [11]
2021-02-15 14:31:54.487 WIT [2021-02-15 07:31:54 +0000] [11] [INFO] Worker exiTing (pID: 11)
2021-02-15 14:31:54.487 WIT ======================================
2021-02-15 14:31:54.487 WIT INFO : Model loaded succesfully
2021-02-15 14:31:54.487 WIT ELAPSED MODEL TIME : 13.514873743057251
2021-02-15 14:31:54.487 WIT INFO : Master Data updated succesfully
2021-02-15 14:31:54.487 WIT ELAPSED DATABASE TIME : 0.52472138@R_403_6939@84619
2021-02-15 14:31:54.487 WIT ======================================
2021-02-15 14:31:54.487 WIT [2021-02-15 07:31:54 +0000] [12] [INFO] Application shutdown complete.
2021-02-15 14:31:54.487 WIT [2021-02-15 07:31:54 +0000] [12] [INFO] Finished server process [12]
2021-02-15 14:31:54.487 WIT [2021-02-15 07:31:54 +0000] [12] [INFO] Worker exiTing (pID: 12)
从我的云运行日志可以看出,我的gunicorn突然关机了。
这是我的错误:
环顾四周后,我尝试了一些方法,例如:
--worker-tmp-dir /dev/shm
(我使用这一行是因为我认为我的 Docker 容器可能被阻塞,所以我添加了这一行以确保没有来自 Docker 容器的阻塞,但它仍然获得 504 状态)。 source 1 source 2
--preload
(我使用这个是因为我认为我的云运行需要节省一些内存才能快速启动我的 Gunicorn,以防我的 Gunicorn 关闭,然后当我再次加载我的页面时,它会加载得更快,但是它仍然没有影响)。 source
worker=2
、thread=4
、graceful_timeout=100
,但它仍然使我的 Cloud Run 关闭。谢谢
默认情况下,Cloud Run 是完全托管的,这意味着实例数量将自动缩放为零。下一个请求将触发一个新实例的启动。此时,您的 Docker 映像将被加载和初始化。这个过程需要时间,称为预热过程。在您的情况下,该过程比您配置的请求超时花费的时间更长。
您有多种改进流程的可能性:
*-alpine
作为基础镜像)apt
和 pip
的缓存gunicorn
并通过 Cloud Run 进行扩展这不是个好主意:
--min-instances
以上是大佬教程为你收集整理的Google Cloud Run 上的 Gunicorn 获得 504 错误状态(上游请求超时)全部内容,希望文章能够帮你解决Google Cloud Run 上的 Gunicorn 获得 504 错误状态(上游请求超时)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。