From 8873aafca31237cd5641ff9c25ec3c8ac07c1e7e Mon Sep 17 00:00:00 2001 From: Emil Melnikov Date: Wed, 9 Dec 2020 20:35:50 +0100 Subject: [PATCH 1/3] Document the lifespan event handler parameter https://github.com/encode/starlette/pull/799 added support for the `lifespan` parameter, but it was not reflected in the documentation. --- docs/events.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/docs/events.md b/docs/events.md index 4f2bce558..77f34aa10 100644 --- a/docs/events.md +++ b/docs/events.md @@ -37,6 +37,25 @@ registered startup handlers have completed. The shutdown handlers will run once all connections have been closed, and any in-process background tasks have completed. +A single lifespan asyncronous generator handler can be used instead of +separate startup and shutdown handlers: + +```python +from starlette.applications import Starlette + + +async def lifespan(app): + # Execute startup tasks. + yield + # Execute shutdown tasks. + +routes = [ + ... +] + +app = Starlette(routes=routes, lifespan=lifespan) +``` + ## Running event handlers in tests You might want to explicitly call into your event handlers in any test setup From e2610951cec2e0fe2c0548b67a38c9d73b530d06 Mon Sep 17 00:00:00 2001 From: Emil Melnikov Date: Thu, 8 Jul 2021 12:48:35 +0200 Subject: [PATCH 2/3] Change lifespan to asynccontextmanager Update docs according to changes in https://github.com/encode/starlette/pull/1227. --- docs/events.md | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/events.md b/docs/events.md index 77f34aa10..f408bdb0e 100644 --- a/docs/events.md +++ b/docs/events.md @@ -37,17 +37,25 @@ registered startup handlers have completed. The shutdown handlers will run once all connections have been closed, and any in-process background tasks have completed. -A single lifespan asyncronous generator handler can be used instead of +A single lifespan asynccontextmanager handler can be used instead of separate startup and shutdown handlers: ```python +import contextlib +import anyio from starlette.applications import Starlette +@contextlib.asynccontextmanager async def lifespan(app): - # Execute startup tasks. - yield - # Execute shutdown tasks. + # acquire async resources + async with anyio.create_task_group() as app.tg: + try: + yield + finally: + with anyio.CancelScope(shield=True): + # release async resources + routes = [ ... From 7d04f65cd72b8d786b77de59be0389daa226ee49 Mon Sep 17 00:00:00 2001 From: Emil Melnikov Date: Mon, 12 Jul 2021 10:37:26 +0200 Subject: [PATCH 3/3] Simplify asynccontextmanager example Simplify asynccontextmanager example, and add a link to anyio docs. --- docs/events.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/docs/events.md b/docs/events.md index f408bdb0e..c7ed49e9d 100644 --- a/docs/events.md +++ b/docs/events.md @@ -48,13 +48,8 @@ from starlette.applications import Starlette @contextlib.asynccontextmanager async def lifespan(app): - # acquire async resources - async with anyio.create_task_group() as app.tg: - try: - yield - finally: - with anyio.CancelScope(shield=True): - # release async resources + async with some_async_resource(): + yield routes = [ @@ -64,6 +59,9 @@ routes = [ app = Starlette(routes=routes, lifespan=lifespan) ``` +Consider using [`anyio.create_task_group()`](https://anyio.readthedocs.io/en/stable/tasks.html) +for managing asynchronious tasks. + ## Running event handlers in tests You might want to explicitly call into your event handlers in any test setup