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
相关推荐
李高钢4 小时前
Python FastAPI 框架入门:从零搭建你的第一个高性能 API 服务
数据库·python·fastapi
杰克尼11 小时前
FastAPI
fastapi
CAE虚拟与现实15 小时前
Uvicorn或者uv是什么?
uv·gunicorn·uvicorn
李高钢15 小时前
Python Flask 框架入门:从零搭建你的第一个 Web 应用
前端·python·flask
NotBeBarnon16 小时前
做 AI 应用总在重复造轮子?我开源了一个 FastAPI 后端脚手架,开箱即用
fastapi
程序员清风1 天前
FastAPI 入门:用 Python 快速开发现代后端服务
python·oracle·fastapi
半亩码田2 天前
【.NET新特性·第17篇】EF Core 10 与 Aspire 13.1
python·flask·.net
萧鼎2 天前
Python 高性能Web框架神器 FastAPI:自动生成API文、基于Pydant、异步请求处理全搞定
前端·python·fastapi
2601_962078033 天前
构建RESTful APIs:使用Python和Flask
python·flask·web开发·api设计·构建restfulapis
the局外人3 天前
学习 FastAPI 的 Day 3:企业级目录与数据库迁移
后端·python·fastapi