Skip to content

Document the lifespan event handler parameter#1110

Merged
graingert merged 4 commits intoKludex:masterfrom
emilmelnikov:patch-1
Jul 15, 2021
Merged

Document the lifespan event handler parameter#1110
graingert merged 4 commits intoKludex:masterfrom
emilmelnikov:patch-1

Conversation

@emilmelnikov
Copy link
Contributor

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

#799 added support for the `lifespan` parameter, but it was not reflected in the documentation.
@JayH5 JayH5 added the documentation Project documentation label Feb 4, 2021
Copy link
Contributor

@graingert graingert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Comment on lines +52 to +57
async with anyio.create_task_group() as app.tg:
try:
yield
finally:
with anyio.CancelScope(shield=True):
# release async resources
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I lead you wrong here - the TaskGroup context manager will capture any CancelledError for you

Suggested change
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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do want to somehow document that yield can now throw CancelledError

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sometimes you want to acquire/release resources inside the TaskGroup and sometimes you want to acquire/release them outside the TaskGroup and there's no specific answer as they both have different behaviours with their own pros/cons

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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():
        yield

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's fair

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the simple lifespan example; we can also add a link to anyio documentation on task groups and cancellation.

@graingert graingert requested a review from uSpike July 8, 2021 11:15
@graingert
Copy link
Contributor

Blocks #1233

Simplify asynccontextmanager example, and add a link to anyio docs.
@emilmelnikov emilmelnikov requested a review from graingert July 12, 2021 08:37
@graingert
Copy link
Contributor

Thanks! Apologies for dragging this one off topic in my original review

@graingert graingert merged commit 8a3e41a into Kludex:master Jul 15, 2021
@graingert graingert mentioned this pull request Jul 15, 2021
11 tasks
graingert added a commit that referenced this pull request Jul 15, 2021
@emilmelnikov emilmelnikov deleted the patch-1 branch July 15, 2021 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Project documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants