FastAPI部署

FastAPI部署

在这里插入图片描述

Uvicorn

  • Uvicorn 是基于 uvloophttptools 构建的非常快速的 ASGI 服务器。
  • Uvicorn 提供一个轻量级的方法来运行多个工作进程,比如 -workers 4 ,但是并没有提供进行的监控。

Gunicorn

  • Gunicorn 是成熟的,功能齐全的服务器,Uvicorn 内部包含有 Gunicornworkers 类,允许你运行 ASGI 应用程序,这些 workers 继承了所有 Uvicorn 高性能的特点,并且给你使用 Gunicorn 来进行进程管理。
  • 用来管理Uvicorn

使用Gunicorn和Uvicorn的worker类生产环境部署

1、Liunx命令行后台启动

nohup gunicorn -c main:app --host 0.0.0.0 --port 8080 -w 4 -k uvicorn.workers.UvicornWorker

2、 docker部署

2.1. Gunicorn配置文件(gunicorn.conf.py)
import multiprocessing

# 是否开启debug
debug = True

# 设置守护进程
# daemon = True

# 绑定ip和端口号
bind = '0.0.0.0:8000'

# 超时时间
timeout = 30

# 工作模式
worker_class = 'uvicorn.workers.UvicornWorker'

# 进程数
workers = multiprocessing.cpu_count() * 2 + 1

# 设置证书
# keyfile = ''
# certfile = ''

# 日志级别,这个日志级别指的是错误日志级别,而访问日志的级别无法设置
loglevel = 'debug'

# 日志配置
# 访问日志文件
accesslog = "/tmp/gunicorn_fasttest_access.log"
# 错误日志文件
errorlog = "/tmp/gunicorn_fasttest_error.log"
# OR
# 配置文件方式配置日志
# logconfig = "./logger.ini"

2.2 Dockerfile文件
  • 需要先进行安装所需系统依赖()
  • 然后安装python依赖包
  • 指定容器中可以暴露的端口(EXPOSE)
  • 运行容器时,需要执行的命令(ENTRYPOINT)
  • -preload 此参数可查看详细的报错信息
FROM python:3.8.5

LABEL author="Desire"
LABEL desc="web fastapi"

COPY . /app

WORKDIR /app

RUN apt-get update && apt-get install -y build-essential gcc libc-dev make python3-lxml \\
&& pip install -r requirements.txt -i <http://mirrors.aliyun.com/pypi/simple>

EXPOSE 8000

ENTRYPOINT ["gunicorn", "--preload", "-c", "gunicorn.conf.py", "main:app"]
2.3 构建镜像
docker build -t fastapi_gunicorn:v1 -f Dockerfile .

2.4 运行容器
docker run -di -p 8000:8000 --name fastapi_gunicorn fastapi_gunicorn:v1

2.5 小拓

1)如果gunicorn.conf.py文件和main.py没有在同一个目录下

目录如下:

├── app
│   └── main.py
├── Dockerfile
├── gunicorn.conf.py
├── requirements.txt
└── start.sh

2)启动的时候可以使用shell脚本(start.sh)进行启动

  • 先进入到main.py目录
  • 指定启动配置文件时使用../gunicorn.conf.py表示上一级目录下的gunicorn.conf.py
cd app
gunicorn --preload -c ../gunicorn.conf.py main:app

3)Dockerfile只需要把启动命令换成执行shell脚本即可

ENTRYPOINT [ "bash", "start.sh" ]
2.6 导包问题

在这里插入图片描述

报错原因:
  • 入口文件放在app文件夹中,启动时的寻包路径就从app开始了,所以找不到包
解决:
  • 把入口文件main.py放到项目根目录下
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 Docker 部署 FastAPI 应用程序,也可以使用 uWSGI 或 Gunicorn 部署。在 Ubuntu 上,可以使用 Nginx 作为反向代理服务器来处理请求。具体的部署步骤可以参考 FastAPI 的官方文档。 ### 回答2: 在Ubuntu上部署FastAPI可以按照以下步骤进行: 1. 首先,确保已经安装了Python和pip。 ``` $ sudo apt update $ sudo apt install python3 $ sudo apt install python3-pip ``` 2. 创建一个新的虚拟环境(可选但推荐)。 ``` $ python3 -m venv myenv $ source myenv/bin/activate ``` 3. 安装FastAPI和uvicorn。 ``` $ pip3 install fastapi $ pip3 install uvicorn ``` 4. 编写一个FastAPI应用程序。 ```python from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} ``` 5. 使用uvicorn运行应用程序。 ``` $ uvicorn main:app --host 0.0.0.0 --port 8000 ``` 这将在本地主机的8000端口上运行应用程序。 6. 若要在生产环境中使用FastAPI,您可以使用Gunicorn作为反向代理服务器。 ``` $ pip3 install gunicorn $ gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app ``` 这将在8000端口上运行FastAPI应用程序,并使用4个工作进程进行请求处理。 通过按照以上步骤,在您的Ubuntu服务器上部署FastAPI应用程序应该是比较简单的。您可以根据您的需求进行进一步配置和调整。 ### 回答3: 要将FastAPI部署在Ubuntu上,可以按照以下步骤进行操作: 1. 确保Ubuntu系统已正确安装和配置。确保系统处于最新更新状态,可以通过运行`sudo apt update && sudo apt upgrade`命令来更新系统。 2. 安装Python和相关依赖。FastAPI是用Python编写的,因此需要在Ubuntu上安装Python及其相关依赖。在终端中运行以下命令安装Python: ``` sudo apt install python3-dev python3-pip ``` 3. 创建并激活虚拟环境(可选)。为了避免与系统中的其他Python软件包发生冲突,可以创建一个虚拟环境来安装和运行FastAPI。在终端中运行以下命令创建虚拟环境: ``` python3 -m venv myenv source myenv/bin/activate ``` 4. 安装FastAPI和其它软件包。在虚拟环境中运行以下命令来安装FastAPI和相应的依赖: ``` pip install fastapi[all] ``` 这将安装FastAPI及其所有附带的依赖,包括uvicorn作为默认的Web服务器。 5. 编写FastAPI应用程序。创建一个Python文件,例如`main.py`,使用FastAPI编写你的应用程序逻辑。例如,你可以创建一个简单的接口,显示一个Hello World消息。示例代码如下: ```python from fastapi import FastAPI app = FastAPI() @app.get("/") async def read_root(): return {"Hello": "World"} ``` 6. 启动FastAPI应用程序。在终端中运行以下命令,启动FastAPI应用程序: ``` uvicorn main:app --host 0.0.0.0 --port 8000 ``` 这将使FastAPI应用程序在本地主机的8000端口上运行。 7. 在浏览器中测试。使用浏览器或任何HTTP客户端工具,访问`http://localhost:8000`,你将看到FastAPI应用程序返回的Hello World消息。 通过按照以上步骤,在Ubuntu上成功部署和运行FastAPI应用程序。这使你能够构建高性能、现代化的Web应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值