Skip to content

Q: will Mangum run lifespan multiple times? #211

Answered by evanandrews-xrd
dimaqq asked this question in Q&A
Discussion options

You must be logged in to vote

I believe I've also run into this.

For every request, mangum runs the startup event, then the handler, then the shutdown event. For applications with expensive startup tasks, this is wasteful and does not match my understanding of ASGI lifespan semantics.

Consider the following FastAPI application:

import asyncio
from fastapi import FastAPI
from mangum import Mangum

app = FastAPI()


@app.on_event("startup")
async def on_startup():
    print("> Starting up")
    await asyncio.sleep(3)  # Simulate expensive startup tasks


@app.get("/")
async def read_root():
    print("> Handling request")
    return "Hello World!"


@app.on_event("shutdown")
async def on_shutdown():
    print("> Shuttin…

Replies: 5 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by dimaqq
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
6 participants
Converted from issue

This discussion was converted from issue #208 on December 09, 2021 12:13.