我有一个异步代码运行在fastapi和aiofiles上,我试图从一个.json文件加载和保存我的信息,但每次我关闭程序时,它只保存字典的密钥,并显示我"ASGI‘寿命’协议似乎不支持“消息
这是我的开关部分:
@app.on_event("startup")
async def startup_event():
global beers
try:
async with aiofiles.open("data.json", mode='r+', json=True) as file:
beers = await file.read()
except:
beers = {}
@app.on_event("shutdown")
async def on_exit_app():
async with aiofiles.open("data.json", "w+") as outfile:
await outfile.write(beers)你知道问题出在哪里吗?
发布于 2021-02-09 03:00:49
这99%意味着on_event("shutdown")函数中的某些东西抛出了服务器(FastAPI/Starlette)没有捕获到的错误,应用程序崩溃,而不是正常结束。这导致uvicorn认为服务器不支持ASGI协议的livespan部分。
如果您使用附加选项--lifespan on运行uvicorn,则会显示错误,您可以对其进行调试。
发布于 2020-10-24 18:55:16
这只是一个断言,你可以忽略这一点,据我所知,你正在使用Uvicorn作为HTTP服务器,因为FastAPI是建立在ASGI框架之上的,而Uvicorn是一个ASGI HTTP服务器,在它上面有一些协议。ASGI协议支持http、websocket。
Uvicorn将lifespan的值设置为auto,断言来自那里。
if self.config.lifespan == "auto":
msg = "ASGI 'lifespan' protocol appears unsupported."但是你可以使用--lifespan on来解决这个问题。
https://stackoverflow.com/questions/64512286
复制相似问题