gunicorn 和 uvicorn部署fastapi 或者flask

gunicorn 和 uvicorn一起部署fastapi 或者flask

安装需要的包

bash 复制代码
pip install gunicorn uvicorn greenlet  eventlet gevent

后台代码

Server.py 文件中有app 变量

启动

bash 复制代码
# http 服务 timeout 单位s
gunicorn -k uvicorn.workers.UvicornWorker    Server:app --bind 0.0.0.0:5665 --workers 2 --timeout 100

# https 服务
gunicorn -k uvicorn.workers.UvicornWorker  --certfile=./configs/a.pem --keyfile=./configs/a-key.pem  Server:app --bind 0.0.0.0:5665 --workers 2 --timeout 520 

可能遇到的问题

如果遇到报错

Error: class uri 'uvicorn.workers.UvicornWorker' invalid or not found:

[Traceback (most recent call last):

File "/usr/lib/python2.7/site-packages/gunicorn/util.py", line 135, in load_class

mod = import_module('.'.join(components))

File "/usr/lib64/python2.7/importlib/init .py", line 37, in import_module
import (name)

ImportError: No module named uvicorn.workers

]

是因为环境中gunicorn 有冲突,需要指定安装环境的gunicorn

bash 复制代码
 /your_env_path/anaconda3/envs/your_env_name/bin/gunicorn -k uvicorn.workers.UvicornWorker  --certfile=./configs/a.pem --keyfile=./configs/a-key.pem  imageSegSimServer:app --bind 0.0.0.0:5665 --workers 2 --timeout 520
相关推荐
海鸥-w2 小时前
用python (fastapi)做项目第一天创建项目结构,数据建表,ORM配置安装,写第一个接口
数据库·python·fastapi
㳺三才人子2 小时前
初探 Flask-WTF
后端·python·flask·html5
Ztopcloud极拓云视角3 小时前
微软Build 2026自研MAI模型全接入指南:用Python搭一个多模型路由网关
python·microsoft·flask
li星野3 小时前
从零搭建文件上传系统:FastAPI 后端 + Streamlit 前端
前端·状态模式·fastapi
至天1 天前
FastAPI 接入 FastAPI-Limiter 以及使用 Redis 进行限流指南
redis·python·fastapi·请求限流
chimchim661 天前
Azure Data Factory (ADF)‌ 之databricks使用
后端·python·flask
li星野1 天前
FastAPI 中间件完全指南:从原理到实战,掌控请求响应的全局钩子
中间件·fastapi
Derrick__11 天前
基于 LangGraph + FastAPI 搭建一个带人工审核的行业分析多智能体系统
ai·agent·fastapi·vibe coding
ss2732 天前
【Python实战】基于FastAPI的绿植养护管理系统 - 完整项目
python·fastapi
li星野2 天前
FastAPI 响应类型完全指南:从 JSON 到流式响应、异常处理与输出模型
前端·json·fastapi