似乎不支持ASGI“寿命"协议 [英] ASGI 'lifespan' protocol appears unsupported
问题描述
我有一个在fastapi&上运行的异步代码.档案我正在尝试从.json文件加载并保存我的信息,但是每次我关闭该程序时,它仅保存字典的键,并向我显示"ASGI'lifespan'协议似乎不受支持".按摩
I have an asynchronous code running on fastapi & aiofiles i'm trying to load and save my info from a .json file but every time I shut down the program, it save only the keys of the dict and showing me "ASGI 'lifespan' protocol appears unsupported" massage
这是我的打开/关闭部分:
this is my turn on/off part:
@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)
有什么问题吗?
推荐答案
此99%表示 on_event("shutdown"))
函数中的某些内容引发了错误,而该错误并未被服务器(FastAPI/Starlette),应用崩溃,而不是正常结束.这使uvicorn认为服务器不支持ASGI协议的有效期部分.
This 99% means that something in the on_event("shutdown")
function throws an error that isn't caught by the server (FastAPI/Starlette), and the app crashes, instead of ending properly. This leads uvicorn to believe that the server doesn't support the livespan part of the ASGI protocol.
如果您运行带有附加选项-lifespan on
的 uvicorn
,则会显示该错误,您可以对其进行调试.
If you run uvicorn
with additional option --lifespan on
, the error will be shown and you can debug it.
请参见 Starlette错误报告.
这篇关于似乎不支持ASGI“寿命"协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!