Skip to content

Commit

Permalink
Document the lifespan event handler parameter (#1110)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Grainger <tagrain@gmail.com>
  • Loading branch information
emilmelnikov and graingert committed Jul 15, 2021
1 parent 537ab6a commit 8a3e41a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/events.md
Expand Up @@ -37,6 +37,31 @@ 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 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):
async with some_async_resource():
yield


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
Expand Down

0 comments on commit 8a3e41a

Please sign in to comment.