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
相关推荐
AI周红伟7 小时前
周红伟:智能体实战,通过使用 Flask 的 REST API 在 Python 中部署 PyTorch
后端·python·flask
PieroPc9 小时前
2026年,我的AI编程助手使用心得(纯个人体验,非评测)
javascript·css·html·fastapi·ai编程
ShoreKiten1 天前
ctfshowweb361--一道题从0入门SSTI模板注入
web安全·flask·ssti·ctfshow
Li emily2 天前
如何获取港股实时行情数据API接口
api·fastapi·港股
kyle~2 天前
Python---Flask 轻量级Web框架
开发语言·python·flask
SmartBrain2 天前
FastAPI 进阶(第二部分):SQLAlchemy ORM(含考题)
数据库·人工智能·aigc·fastapi
岱宗夫up3 天前
FastAPI入门(上篇):快速构建高性能Python Web API
开发语言·前端·python·fastapi
SmartBrain3 天前
FastAPI 与 Langchain、Coze、Dify 技术深度对比分析
java·架构·fastapi
曲幽3 天前
FastAPI 实战:WebSocket 从入门到上线,使用避坑指南
python·websocket·fastapi·web·async·asyncio
xdpcxq10293 天前
Flask - 常见应用部署方案
后端·python·flask