Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 475
yuval hayun Avatar asked Oct 24 '20 10:10

yuval hayun


1 Answers

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.

If you run uvicorn with additional option --lifespan on, the error will be shown and you can debug it.

See Starlette bug report.

like image 181
M. Volf Avatar answered Sep 21 '22 09:09

M. Volf