簡體   English   中英

Docker:通過 Gunicorn 運行 Flask 應用程序 - 工作人員超時? 表現不佳?

[英]Docker: Running a Flask app via Gunicorn - Worker timeouts? Poor performance?

我正在嘗試創建一個用 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()

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM