MCPcopy
hub / github.com/encode/starlette / lifespan

Method lifespan

starlette/routing.py:629–654  ·  starlette/routing.py::Router.lifespan

Handle ASGI lifespan messages, which allows us to manage application startup and shutdown events.

(self, scope: Scope, receive: Receive, send: Send)

Source from the content-addressed store, hash-verified

627 raise NoMatchFound(name, path_params)
628
629 async def lifespan(self, scope: Scope, receive: Receive, send: Send) -> None:
630 class="st">"""
631 Handle ASGI lifespan messages, which allows us to manage application
632 startup and shutdown events.
633 class="st">"""
634 started = False
635 app: Any = scope.get(class="st">"app")
636 await receive()
637 try:
638 async with self.lifespan_context(app) as maybe_state:
639 if maybe_state is not None:
640 if class="st">"state" not in scope:
641 raise RuntimeError(&class="cm">#x27;The server does not support class="st">"state" in the lifespan scope.')
642 scope[class="st">"state"].update(maybe_state)
643 await send({class="st">"type": class="st">"lifespan.startup.complete"})
644 started = True
645 await receive()
646 except BaseException:
647 exc_text = traceback.format_exc()
648 if started:
649 await send({class="st">"type": class="st">"lifespan.shutdown.failed", class="st">"message": exc_text})
650 else:
651 await send({class="st">"type": class="st">"lifespan.startup.failed", class="st">"message": exc_text})
652 raise
653 else:
654 await send({class="st">"type": class="st">"lifespan.shutdown.complete"})
655
656 async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
657 class="st">"""

Callers 1

appMethod · 0.95

Calls 4

receiveFunction · 0.50
sendFunction · 0.50
getMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected