gpt4 book ai didi

python - Docker:通过 Gunicorn 运行 Flask 应用程序 - Worker 超时?表现不佳?

转载 作者:行者123 更新时间:2023-12-01 06:52:03 25 4
gpt4 key购买 nike

我正在尝试创建一个用 Python Flask 编写的新应用程序,由 Gunicorn 运行,然后进行 Docker 化。

我遇到的问题是 docker 容器内的性能非常差,不一致,我最终得到了响应,但我不明白为什么性能会下降。有时我会在日志中看到[CRITICAL] WORKER TIMEOUT (pid:9)

这是我的应用程序:

server.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
return "The server is running!"

if __name__ == '__main__':
app.run()

Dockerfile

FROM python:3.6.9-slim

# Copy all the files to the src folder
COPY build/ /usr/src/
# Create the virtual environment
RUN python3 -m venv /usr/src/myapp_venv
# Install the requirements
RUN /usr/src/myapp_venv/bin/pip3 install -r /usr/src/requirements.txt
# Runs gunicorn
# --chdir sets the directory where gunicorn should look for the server files
# server:app means run the "server.py" file and look for the "app" constructor within that
ENTRYPOINT ["/usr/src/myapp/bin/gunicorn", "--bind", "0.0.0.0:5000", "--workers", "1", "--chdir", "/usr/src/", "server:app"]
# Expose the gunicorn port
EXPOSE 5000

需求.txt

Click==7.0
Flask==1.1.1
gunicorn==20.0.0
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
Werkzeug==0.16.0

我像这样运行 docker 容器:

docker build -t killerkode/myapp .
docker run --name myapp -p 5000:5000 killerkode/myapp

最佳答案

我设法找到了这篇有用的文章,它解释了 Gunicorn 挂起的原因。 https://pythonspeed.com/articles/gunicorn-in-docker/

我的解决方案是更改工作人员临时目录并将最小工作人员数量增加到 2。我仍然看到工作人员被杀死,但不再有任何延迟/缓慢。我怀疑添加 gthread 会进一步改善情况。

这是我更新的 Dockerfile:

FROM python:3.6.9-slim

# Copy all the files to the src folder
COPY build/ /usr/src/
# Create the virtual environment
RUN python3 -m venv /usr/src/myapp_venv
# Install the requirements
RUN /usr/src/myapp_venv/bin/pip3 install -r /usr/src/requirements.txt
# Runs gunicorn
# --chdir sets the directory where gunicorn should look for the server files
# server:app means run the "server.py" file and look for the "app" constructor within that
ENTRYPOINT ["/usr/src/myapp/bin/gunicorn", "--bind", "0.0.0.0:5000", "--worker-tmp-dir", "/dev/shm", "--workers", "2", "--chdir", "/usr/src/", "server:app"]
# Expose the gunicorn port
EXPOSE 5000

关于python - Docker:通过 Gunicorn 运行 Flask 应用程序 - Worker 超时?表现不佳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58942398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com