[英]ASGI 'lifespan' protocol appears unsupported
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我有一个在 fastapi 和 aiofiles 上运行的异步代码,我试图从 .json 文件加载和保存我的信息,但是每次我关闭程序时,它只保存字典的键并显示“ASGI 'lifespan'协议似乎不受支持”按摩
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)
any ideas where is the problem?任何想法问题出在哪里?
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.这 99% 意味着
on_event("shutdown")
函数中的on_event("shutdown")
引发了服务器(FastAPI/Starlette)未捕获的错误,并且应用程序崩溃,而不是正确结束。 This leads uvicorn to believe that the server doesn't support the livespan part of the ASGI protocol.这导致 uvicorn 相信服务器不支持 ASGI 协议的 livespan 部分。
If you run uvicorn
with additional option --lifespan on
, the error will be shown and you can debug it.如果您使用附加选项
--lifespan on
运行uvicorn
,则会显示错误,您可以对其进行调试。
See Starlette bug report .请参阅Starlette 错误报告。
It is just an assertion you can omit that, as far as I understand you are using Uvicorn as an HTTP server, since FastAPI is built top on an ASGI framework and Uvicorn is an ASGI HTTP server, there are some protocols on it.这只是一个你可以省略的断言,据我所知,你使用 Uvicorn 作为 HTTP 服务器,因为 FastAPI 构建在 ASGI 框架之上,而 Uvicorn 是一个 ASGI HTTP 服务器,它上面有一些协议。 ASGI protocol has support for http, websocket.
ASGI 协议支持 http、websocket。
Uvicorn sets lifespan's value to auto
and the assertion comes from there. Uvicorn 将 lifespan 的值设置为
auto
并且断言来自那里。
if self.config.lifespan == "auto":
msg = "ASGI 'lifespan' protocol appears unsupported."
But you can use --lifespan on
to fix that.但是你可以使用
--lifespan on
来解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.