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)
any ideas where is the problem?
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. ASGI protocol has support for http, websocket.
Uvicorn sets lifespan's value to
autoand the assertion comes from there.But you can use
--lifespan onto fix that.