python3的uvicorn模块

发布时间 2023-09-20 16:48:21作者: 凯-子
FastAPI和uvicorn 是一个用于构建 API 的现代、快速(高性能)的 web 框架,使用 Python 3.6+ 并基于标准的 Python 类型提示。
举例1:
import uvicorn
from fastapi import FastAPI


app = FastAPI()

# 绑定路由和视图函数
@app.get("/")
def index():
    return {"msg": "fastapi测试成功"}

# 异步 后面基本会用异步 速度快
@app.get("/A")
async def A():
    return {"msg": "异步请求成功"}

# 同步
@app.get("/B")
def B():
    return {"msg": "请求成功"}

### 动态路径 和 Flask 不同,Flask 是使用 <>,而 FastAPI 使用 {}
@app.get("/C/{item_id}")
def C(item_id):
    return {"msg": item_id}

if __name__ == "__main__":
    uvicorn.run(app,host="0.0.0.0",port=8000)
View Code