FastAPI的两种启动方式

来个简单的Hello world 例子

import uvicorn
from fastapi import FastAPI

app = FastAPI()


@app.get('/')
async def root():
    return {"msg": "Hello World"}

if __name__ == '__main__':
    uvicorn.run('test02:app')

第一种:通过uvicorn.run()启动,其中test02为当前py文件,app为FastAPI的实例对象,这样启动默认为http://127.0.0.1:8000,当然也可以更改host和port

 

 

 

第二种:通过命令uvicorn test02:app --reload      其中--reload :可以自动重启

 

posted @ 2022-03-17 10:19  手可摘星辰/*  阅读(6813)  评论(0编辑  收藏  举报