Handle ASGI lifespan messages, which allows us to manage application startup and shutdown events.
(self, scope: Scope, receive: Receive, send: Send)
| 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">""" |