# lifespan

URL: /pounce/api/asgi/lifespan/
Section: asgi
Description: ASGI lifespan handler.

Manages the ASGI lifespan protocol — sends startup/shutdown events to the
application and waits for completion. Used as an async context manager so
the server can bracket its run loop with lifespan events.

Handles apps that don't support lifespan — whether they raise an
exception or silently return for non-HTTP scopes.

---

> For a complete page index, fetch /pounce/llms.txt.

Open LLM text
(/pounce/api/asgi/lifespan/index.txt)

Share with AI

Ask Claude
(https://claude.ai/new?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fpounce%2Fapi%2Fasgi%2Flifespan%2Findex.txt)

Ask ChatGPT
(https://chatgpt.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fpounce%2Fapi%2Fasgi%2Flifespan%2Findex.txt)

Ask Gemini
(https://gemini.google.com/app?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fpounce%2Fapi%2Fasgi%2Flifespan%2Findex.txt)

Ask Copilot
(https://copilot.microsoft.com/?q=Please%20help%20me%20understand%20this%20documentation%3A%20%2Fpounce%2Fapi%2Fasgi%2Flifespan%2Findex.txt)

Module

#
`asgi.lifespan`

ASGI lifespan handler.

Manages the ASGI lifespan protocol — sends startup/shutdown events to the
application and waits for completion. Used as an async context manager so
the server can bracket its run loop with lifespan events.

Handles apps that don't support lifespan — whether they raise an
exception or silently return for non-HTTP scopes.

1Function

## Functions

`run_lifespan`

2

`AsyncIterator[dict[str, …`

▼

Run the ASGI lifespan protocol as an async context manager.

Sends lifespan.sta…

async

`async def run_lifespan(app: ASGIApp, config: ServerConfig) -> AsyncIterator[dict[str, Any]]`

Run the ASGI lifespan protocol as an async context manager.

Sends lifespan.startup on entry, waits for the app to respond with
lifespan.startup.complete, yields control to the caller, then sends
lifespan.shutdown on exit.

If the app doesn't support lifespan (raises an exception or returns
silently during startup), the lifespan is treated as a no-op.

The lifespan scope includes a "state" dict that the app can populate
during startup. This same dict is returned to the caller and should
be injected into all request scopes as scope["state"] (ASGI 3.0 spec).

##### Parameters

Name
Type
Description

`app`
`ASGIApp`

The ASGI application.

`config`
`ServerConfig (/pounce/api/config/#ServerConfig)`

Server configuration.

##### Returns

`AsyncIterator[dict[str, Any]]`
