Skip to content

Proper way to run code on startup and shutdown #1391

Answered by specd-tech
specd-tech asked this question in Q&A
Discussion options

You must be logged in to vote

I found a working solution, I believe this is the correct way to use lifespan events. If anyone has any improvements they would be greatly appreciated.

async def app(scope, receive, send):
    if scope["type"] == "http":
        await send(
            {
                "type": "http.response.start",
                "status": 200,
                "headers": [
                    [b"content-type", b"text/plain"],
                ],
            }
        )
        await send(
            {
                "type": "http.response.body",
                "body": b"Hello, world!",
            }
        )

    elif scope["type"] == "lifespan":
        while True:
            message = await recei…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
2 replies
@specd-tech
Comment options

@euri10
Comment options

Comment options

You must be logged in to vote
2 replies
@euri10
Comment options

@specd-tech
Comment options

Answer selected by specd-tech
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant