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
相关推荐
曲幽5 小时前
FastAPI + PostgreSQL 实战:给应用装上“缓存”和“日志”翅膀
redis·python·elasticsearch·postgresql·logging·fastapi·web·es·fastapi-cache
曲幽20 小时前
FastAPI + PostgreSQL 实战:从入门到不踩坑,一次讲透
python·sql·postgresql·fastapi·web·postgres·db·asyncpg
曲幽2 天前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic
stark张宇4 天前
构建第一个AI聊天机器人:Flask+DeepSeek+Postgres实战
人工智能·postgresql·flask
曲幽6 天前
FastAPI流式输出实战与避坑指南:让AI像人一样“边想边说”
python·ai·fastapi·web·stream·chat·async·generator·ollama
曲幽7 天前
不止于JWT:用FastAPI的Depends实现细粒度权限控制
python·fastapi·web·jwt·rbac·permission·depends·abac
曲幽8 天前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
曲幽9 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
曲幽10 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
曲幽11 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama