fastapi + uvicorn + gunicorn部署时run命令(20210308在用)

本文介绍了使用Docker部署FastAPI应用的详细步骤,包括基于uvicorn和gunicorn的配置,以及优化设置如健康检查、超时管理和镜像加速。同时,还展示了PHP和Node.js应用的Docker运行命令,以及Docker网络创建和IP查找的方法,旨在提升容器化部署的效率和稳定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fastapi + uvicorn + gunicorn部署时run命令(20210308在用)

run docker

docker run -d \
--name mqsub \
--restart=always \
--network php-net \
-p 8000:80 \
-v /root/mqsub:/app \
-e KEEP_ALIVE="20" \
-e GRACEFUL_TIMEOUT="20" \
-e TIMEOUT="20" \
mqsub /start-reload.sh

fastapi uvicorn 部署Dockerfile(20210308在用)

#FROM centos/python-36-centos7
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7
MAINTAINER sam
USER root

WORKDIR /app

# 下载pip包
RUN python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple

RUN pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple

RUN pip install wheel -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install pycrypto -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install mq_http_sdk  -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install aliyunsdkcore -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple aliyun-python-sdk-iot

RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple fastapi
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pydantic
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple uvicorn

#COPY requirements.txt /home/requirements.txt

# 使用淘宝的npm镜像
#RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
#RUN pip install -r requirements.txt

#ENTRYPOINT [ "/bin/bash", "/home/entry-point.sh"]
#ENTRYPOINT  uwsgi --ini /home/hello.ini
#CMD ["uwsgi","--ini", "/home/hello.ini"] #启动uwsgi

nodejs的docker运行命令

docker run -d \
    --restart=always \
    -v /root/xy.api:/app \
    --network php-net \
    -p 3000:3000 \
    --name xyys.nodejs \   #镜像名
    xyys.nodejs \          #容器名
    node /app/app.js    #运行命令

php的docker运行命令

docker run -d \
--restart=always \
--name php-web \
--network php-net \
-p 9001:9000 \
-v /www/nginx/apps:/usr/share/nginx/html \
php:5.6-fpm

建立镜像命令

sudo docker build -t mqsub .

查看docker ip地址

#查看IP;
[root@localhost ~]# docker inspect myphp-fpm | grep "IPAddress"
 "SecondaryIPAddresses": null,
 "IPAddress": "172.17.0.3",
 "IPAddress": "172.17.0.3",
#创建一个docker网络,使用docker来连接多个容器
[root@localhost ~]# docker network create -d bridge php-net
#加入docker网络的容器执行 docker run 命令时要加参数:--network
--network php-net \

docker镜像image拉取速度过慢 - 仓库源:中科大速度最快(docker pull)

#创建、修改配置文件: /etc/docker/daemon.json 
vim /etc/docker/daemon.json          # 编辑daemon.json 
{
    "registry-mirrors": ["http://docker.mirrors.ustc.edu.cn"] 
} 
systemctl restart docker                  # 重启docker服务

如何改变

*强调

适合你的列表

  • 项目
    • 项目
      • 项目
  1. 项目1
  2. 项目2
  3. 项目3
  • 计划任务
  • 完成任务

一个表格

一个简单的表格是这么创建的:

项目Value
电脑$1600
手机$12
导管$1
<think>我们注意到用户提到的版本号(FastAPI 4.1.1, Uvicorn 4.1.2, Gunicorn 4.1.3)并不存在,但用户可能是在寻找与这些版本号相关的大模型技术栈学习资料。实际上,这些工具的最新稳定版本要低得多(FastAPI 0.109.x, Uvicorn 0.29.x, Gunicorn 21.x)。因此,我们将提供基于实际版本的学习资料,并假设用户对构建大模型服务(如LLM API服务)感兴趣。 学习资料将围绕以下主题: 1. 使用FastAPI构建大模型API服务 2. 使用UvicornGunicorn部署高性能ASGI应用 3. 大模型服务特有的优化技巧(如批处理、流式响应、中间件等) 注意:由于版本号错误,我们调整到实际可用版本。</think>### FastAPIUvicornGunicorn 大模型技术栈学习资料 以下学习资料针对**实际可用版本**(FastAPI 0.109.x, Uvicorn 0.29.x, Gunicorn 21.x),涵盖大模型服务部署的核心技术栈: --- ### 一、核心学习路径 #### 1. **FastAPI 深度实践** - **官方文档**:[FastAPI Documentation](https://fastapi.tiangolo.com/) - 异步请求处理 - Pydantic V2 数据验证 - WebSocket 实通信 - **大模型服务开发**: ```python from fastapi import FastAPI from transformers import pipeline app = FastAPI() llm_pipeline = pipeline("text-generation", model="gpt-3.5-turbo") @app.post("/generate") async def generate_text(prompt: str): return llm_pipeline(prompt, max_length=100)[0] ``` #### 2. **Uvicorn 高性能优化** - **关键配置**: ```bash uvicorn main:app --workers 4 --http h11 --ws websockets \ --timeout-keep-alive 60 --limit-concurrency 100 ``` - `--http h11`:HTTP/1.1 协议优化 - `--limit-concurrency`:防止大模型请求过载[^1] - **异步任务管理**: $$ \text{吞吐量} = \frac{\text{工作进程数} \times \text{并发连接数}}{\text{平均响应间}} $$ #### 3. **Gunicorn 生产级部署** - **大模型服务配置模板**: ```bash gunicorn main:app -w 8 -b 0.0.0.0:8000 \ -k uvicorn.workers.UvicornWorker \ --max-requests 1000 --timeout 300 ``` - `-w 8`:按 CPU 核心数配置工作进程 - `--max-requests`:防止内存泄漏(关键!)[^1] --- ### 二、进阶学习资源 | 主题 | 资源 | 重点内容 | |------|------|----------| | **大模型流式响应** | [FastAPI StreamingResponse](https://fastapi.tiangolo.com/advanced/custom-response/#streamingresponse) | SSE(Server-Sent Events) 实现 | | **GPU 资源管理** | [NVIDIA Triton + FastAPI](https://github.com/triton-inference-server/server) | 模型并行推理 | | **分布式部署** | [Kubernetes 配置模板](https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker) | HPA 自动扩缩容 | --- ### 三、性能调优公式 大模型服务的关键性能指标: $$ \text{QPS} = \frac{\text{工作进程数} \times \text{每秒请求数}}{\text{平均响应()}} $$ 优化建议: 1. 当 $ \text{GPU 利用率} > 80\% $ 增加 `-w` 参数 2. 当 $ \text{内存占用} > 70\% $ 降低 `--max-requests` --- ### 四、实验环境搭建 ```dockerfile FROM python:3.10 RUN pip install fastapi==0.109.1 uvicorn==0.29.0 gunicorn==21.2.0 COPY ./app /app CMD ["gunicorn", "app.main:app", "-k", "uvicorn.workers.UvicornWorker"] ``` --- ### 相关问题 1. 如何监控 FastAPI 服务的 GPU 内存使用情况? 2. Uvicorn 的 `--limit-concurrency` 参数如何影响大模型服务的吞吐量? 3. 在 Kubernetes 中如何配置 Gunicorn 工作进程实现零停机更新? 4. FastAPI 的依赖注入系统如何优化大模型服务的初始化间? 5. 如何设计大模型 API 的限流策略防止资源耗尽?[^1]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值