Skip to content

Commit c591c69

Browse files
committed
app
1 parent 840142f commit c591c69

File tree

6 files changed

+352
-17
lines changed

6 files changed

+352
-17
lines changed

Dockerfile

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@
22

33
FROM python:3.11-slim-bookworm
44

5-
WORKDIR /app
5+
# 建议:将工作目录改为 /src 或 /code,避免和 app 包名混淆
6+
WORKDIR /src
67

7-
# Install uv (better: copy the static binary from the official uv image)
8+
# --- 1. 安装依赖环境 ---
89
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
9-
10-
# Optional but common: keep uv caches in a known place
1110
ENV UV_CACHE_DIR=/root/.cache/uv
1211

13-
# Copy only dependency inputs first for better Docker layer caching
1412
COPY pyproject.toml uv.lock ./
15-
16-
# Create/sync the project environment from the lockfile
17-
# --frozen: fail if lock and project metadata don't match; don't update the lock
18-
# --no-dev: skip dev dependency groups in the final image
1913
RUN --mount=type=cache,target=/root/.cache/uv \
2014
uv sync --frozen --no-dev
2115

22-
# Now copy the rest of the app
23-
COPY tests/mock_server.py ./main.py
16+
# --- 2. 复制代码 ---
17+
# 将本地的 app 目录 复制到 容器的 /src/app
18+
COPY app ./app
19+
# 复制配置文件
20+
COPY gunicorn.conf.py .
2421

25-
# Make sure the venv's executables are on PATH
26-
ENV PATH="/app/.venv/bin:$PATH"
22+
ENV PATH="/src/.venv/bin:$PATH"
2723

2824
EXPOSE 8000
29-
CMD ["uv", "run", "--frozen", "python", "main.py"]
25+
26+
# --- 3. 启动命令 ---
27+
# app.main:app 代表:包名(app) -> 文件名(main) -> 实例名(app)
28+
CMD ["gunicorn", "-c", "gunicorn.conf.py", "app.main:app"]

app/__init__.py

Whitespace-only changes.
File renamed without changes.

gunicorn.conf.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import multiprocessing
2+
import os
3+
4+
# 获取 CPU 核心数
5+
workers_per_core = 2
6+
cores = multiprocessing.cpu_count()
7+
8+
# 核心计算公式:CPU核数 * 2 + 1
9+
# 这是一个经典的 Gunicorn 推荐公式,既能利用多核,又能防止过多的上下文切换
10+
default_workers = (cores * workers_per_core) + 1
11+
12+
# 允许通过环境变量覆盖(可选,方便调试)
13+
workers = int(os.getenv("WORKERS", default_workers))
14+
15+
# 指定 Worker 类型为 Uvicorn(关键!必须支持 ASGI)
16+
worker_class = "uvicorn.workers.UvicornWorker"
17+
18+
# 绑定地址
19+
bind = "0.0.0.0:8000"
20+
21+
# 日志级别
22+
loglevel = "warning"
23+
24+
# 超时设置(根据 DeepSeek 的流式输出特点,建议稍微调大,防止长文本生成断连)
25+
timeout = 120
26+
keepalive = 5

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ requires-python = ">=3.11"
77
dependencies = [
88
"black>=25.12.0",
99
"fastapi>=0.128.0",
10+
"gunicorn>=23.0.0",
1011
"openai>=2.14.0",
1112
"pydantic>=2.12.5",
1213
"pytest-xdist>=3.8.0",
13-
"uvicorn>=0.40.0",
14+
"uvicorn[standard]>=0.40.0",
1415
]

0 commit comments

Comments
 (0)