unvicorn旧版本的时候,例如0.10.0,利用fastapi和uvicorn搭建服务如图所示:
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.post('/detector')
......
if __name__=="__main__":
uvicorn.run(app=app,host='10.15.72.22',port=8084,reload=True,workers=1)
unvicorn新版本后,出现
WARNING: You must pass the application as an import string to enable 'reload' or 'workers'
的问题,实际上是调用方式改变了。
我们在detector.py 文件下,启动uvicorn,使用如下命令
from fastapi import FastAPI
import uvicorn
app = FastAPI()
@app.post('/detector')
......
if __name__=="__main__":
uvicorn.run("detector:app",host='10.15.72.22',port=8084,reload=True,workers=1)
detector是文件名 ,app是应用程序实例名字,这样uvicorn
就会正确地加载应用程序并启动服务器。