FastAPI(3)- uvicorn.run()

简介: FastAPI(3)- uvicorn.run()

Uvicorn


  • 基于 uvloop 和 httptools 构建的非常快速的 ASGI 服务器
  • 不是一个 Web 框架,而是一个服务器
  • 例如,它不是一个提供路径路由的框架,这是 FastAPI 框架提供的东西
  • 它是 Starlette 和 FastAPI 的推荐使用的服务器

 

总结

uvicorn 是运行 FastAPI 应用程序的主要 Web 服务器,uvicorn 和 Gunicorn 结合使用,拥有一个异步多进程服务器

 

什么是 ASGI、WSGI


https://www.cnblogs.com/poloyy/p/15291403.html

 

最简单的 FastAPI 代码


from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
    return {"message": "Hello World"}


启动 uvicorn


进到 py 文件所处目录下的命令行运行

uvicorn main:app

image.png

能不能不用命令行方式运行呢,否则太不方便了

可以!

 

使用 uvicorn.run()


from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
    return {"message": "Hello World"}
if __name__ == '__main__':
    uvicorn.run(app="2_get:app", host="127.0.0.1", port=8080, reload=True, debug=True)


  • 这样就不用敲命令行啦
  • uvicorn 有什么命令行参数,run() 方法就有什么参数

 

uvicorn 常用参数


参数 作用
app 运行的 py 文件:FastAPI 实例对象
host 访问url,默认 127.0.0.1
port 访问端口,默认 8080
reload 热更新,有内容修改自动重启服务器
debug 同 reload
reload_dirs 设置需要 reload 的目录,List[str] 类型
log_level 设置日志级别,默认 info

用到什么再补吧,暂时只用上这么多

相关文章
|
机器学习/深度学习 缓存 Kubernetes
FastAPI(62)- FastAPI 部署在 Docker
FastAPI(62)- FastAPI 部署在 Docker
1204 0
FastAPI(62)- FastAPI 部署在 Docker
|
4天前
|
Python
Python Playwright 打包报错 Please run the following command to download new browsers
Python Playwright 打包报错 Please run the following command to download new browsers
108 0
|
7月前
|
数据可视化 Python
常见的bug---5、在安装superset时候报错,Command “python setup.py egg_info“ failed with error code 1
常见的bug---5、在安装superset时候报错,Command “python setup.py egg_info“ failed with error code 1
|
9月前
|
JavaScript
Browserslist: caniuse-lite is outdated. Please run: npx ....
Browserslist: caniuse-lite is outdated. Please run: npx ....
246 0
|
机器学习/深度学习 人工智能 数据挖掘
FastAPI(7)- 详解 Path(上)
FastAPI(7)- 详解 Path(上)
213 0
FastAPI(7)- 详解 Path(上)
|
JSON 数据库 数据格式
FastAPI(46)- JSONResponse
FastAPI(46)- JSONResponse
297 0
FastAPI(46)- JSONResponse
|
JSON JavaScript API
FastAPI(1)- 简单介绍
FastAPI(1)- 简单介绍
274 0
|
JSON API 数据格式
FastAPI(6)- 详解 Query (下)
FastAPI(6)- 详解 Query (下)
274 0
FastAPI(6)- 详解 Query (下)
|
IDE 开发工具
FastAPI(6)- 详解 Query (上)
FastAPI(6)- 详解 Query (上)
176 0
FastAPI(6)- 详解 Query (上)