Document the lifespan event handler parameter#1110
Document the lifespan event handler parameter#1110graingert merged 4 commits intoKludex:masterfrom emilmelnikov:patch-1
Conversation
#799 added support for the `lifespan` parameter, but it was not reflected in the documentation.
graingert
left a comment
There was a problem hiding this comment.
lifespan now takes an async context manager factory, eg an async generator function decorated with @contextlib(2).asynccontextmanager
Update docs according to changes in #1227.
docs/events.md
Outdated
| async with anyio.create_task_group() as app.tg: | ||
| try: | ||
| yield | ||
| finally: | ||
| with anyio.CancelScope(shield=True): | ||
| # release async resources |
There was a problem hiding this comment.
sorry I lead you wrong here - the TaskGroup context manager will capture any CancelledError for you
| async with anyio.create_task_group() as app.tg: | |
| try: | |
| yield | |
| finally: | |
| with anyio.CancelScope(shield=True): | |
| # release async resources | |
| async with anyio.create_task_group() as app.tg: | |
| yield | |
| # release async resources |
There was a problem hiding this comment.
I do want to somehow document that yield can now throw CancelledError
There was a problem hiding this comment.
I'm not sure we should have the main documentation for lifespan be setting app.tg. That could be a separate topic, but I think the initial introduction to "make a lifespan" example should be as simple as possible.
@contextlib.asynccontextmanager
async def lifespan(app):
async with some_async_resource():
yieldThere was a problem hiding this comment.
I like the simple lifespan example; we can also add a link to anyio documentation on task groups and cancellation.
|
Blocks #1233 |
Simplify asynccontextmanager example, and add a link to anyio docs.
|
Thanks! Apologies for dragging this one off topic in my original review |

#799 added support for the
lifespanparameter, but it was not reflected in the documentation.