Kil*_*ode 4 python pip flask gunicorn docker
我正在尝试创建一个用 Python Flask 编写的新应用程序,由 gunicorn 运行然后 dockerised。
我遇到的问题是 docker 容器内的性能非常差、不一致,我最终得到了响应,但我不明白为什么性能下降。有时我在日志中看到[CRITICAL] WORKER TIMEOUT (pid:9)。
这是我的应用程序:
服务器.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "The server is running!"
if __name__ == '__main__':
app.run()
Run Code Online (Sandbox Code Playgroud)
文件
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
Run Code Online (Sandbox Code Playgroud)
要求.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
Run Code Online (Sandbox Code Playgroud)
我像这样运行docker容器:
docker build -t killerkode/myapp .
docker run --name myapp -p 5000:5000 killerkode/myapp
Run Code Online (Sandbox Code Playgroud)
我设法找到了这篇有用的文章,它解释了 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
965 次 |
| 最近记录: |