Skip to content

Deployment

开发与生产的运行方式不同。生产环境使用 WSGI/ASGI 服务器并做反向代理。

选型:

  • WSGI:Gunicorn + gevent / Meinheld,或 Waitress(Windows)
  • ASGI:Uvicorn + asgiref(Flask 3 提供实验性 async 视图支持)
  • 前置 Nginx/Apache 做反向代理、TLS、静态资源

Gunicorn 示例:

bash
pip install gunicorn gevent
export FLASK_APP=app:create_app
gunicorn -w 4 -k gevent -b 0.0.0.0:8000 "app:create_app()"

容器化:

dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:8000", "app:create_app()"]

配置:

  • 通过环境变量注入敏感信息
  • 打开健康检查与就绪探针
  • 使用进程管理/守护(systemd/supervisor)