fastapi

fastapi

FastApi官网:https://fastapi.tiangolo.com/zh/
    
-FastAPI 是一个用于构建 API 的现代、快速(高性能)的 web 框架,使用 Python 3.6+ 并基于标准的 Python 类型提示。
-可与 NodeJS 和 Go 并肩的极高性能(归功于 Starlette 和 Pydantic)。最快的 Python web 框架之一。
# pip install fastapi
# pip install uvicorn
import time
from fastapi import FastAPI
app = FastAPI()

@app.get('/')  # 向根路径发送请求
async def index():  
    return {'code': 100, 'msg': '成功'}

# 运行服务器命令:uvicorn py文件名:app --reload
# 启动后,我们修改代码会自动重启,监听8000端口
=============
# 右键启动方法
if __name__ == '__main__':
    uvicorn.run(app)
import time
from fastapi import FastAPI
app = FastAPI()

@app.get('/') 
async def index():  # 加上async,就是协程函数
    await time.sleep(3)
    return {'code': 100, 'msg': '成功'}

@app.get('/home')
async def home():
    time.sleep(2)
    return {'code': 100, 'msg': 'home'}

@app.get('/order')
async def order():
    time.sleep(2)
    return {'code': 100, 'msg': 'order'}

# 如果是django,flask,这种同步框架,他会开启三个线程来处理这三个请求
# fastapi,sanic这种框架,就只用一个线程处理这三个请求(单线程下的并发),开启第一个遇到IO操作,就会切换,去运行第二个函数。

fastapi自带测试文档

#交互式文档,由Swagger UI 生成
#  http://127.0.0.1:8000/docs

# 一个自动生成的文档(由ReDoc生成)
#  http://127.0.0.1:8000/redoc

 

posted @ 2023-09-15 16:31  刹爵  阅读(259)  评论(0)    收藏  举报