Skip to content

use an async context manager factory for lifespan#1227

Merged
graingert merged 8 commits intoKludex:masterfrom
graingert:use-a-context-manager-factory-for-lifespan
Jul 3, 2021
Merged

use an async context manager factory for lifespan#1227
graingert merged 8 commits intoKludex:masterfrom
graingert:use-a-context-manager-factory-for-lifespan

Conversation

@graingert
Copy link
Contributor

@graingert graingert commented Jun 30, 2021

return gen_wrapper


def _wrap_lifespan_context(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this stuff is a bit of a huge mess I think it's better to just make this a breaking change and make people add contextlib.asynccontextmanager to their functions

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tomchristie are you happy for this to be a breaking change?

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you explain what would be necessary for a user to convert their existing lifespan generator to an asynccontextmanager? i.e. an example?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You just add the @contextlib.asynccontextmanager decorator

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@JayH5 thank you so much for this comment! Because of this I worked out I could get rid of all my ugly _wrap_lifespan_context stuff and just use asynccontextmanager(lifespan_context)

@JayH5
Copy link
Contributor

JayH5 commented Jul 1, 2021

Kinda sorta related (at least involves some similar code): #1205

@graingert graingert marked this pull request as ready for review July 3, 2021 10:35
@graingert graingert requested a review from JayH5 July 3, 2021 10:35
Copy link
Contributor

@JayH5 JayH5 left a comment

Choose a reason for hiding this comment

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

Was going to suggest updating the documentation, but now I see that we don't have docs for the generator lifespan stuff. It therefore seems likely to me that most users are using on_startup/on_shutdown, so the deprecation shouldn't affect too many.

Am happy with this PR and it's a nice clean up.

elif inspect.isasyncgenfunction(lifespan):
warnings.warn(
"async generator function lifespans are deprecated, "
"use an @contextlib.asynccontextmanager function instead",
Copy link
Contributor

Choose a reason for hiding this comment

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

I now read this like it should be "a contextlib..."

Suggested change
"use an @contextlib.asynccontextmanager function instead",
"use a @contextlib.asynccontextmanager function instead",

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 read this as an at contextlib.asynccontextmanager

Copy link
Contributor

@JayH5 JayH5 left a comment

Choose a reason for hiding this comment

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

Nice 👍

@graingert graingert requested a review from JayH5 July 3, 2021 17:34
@graingert graingert merged commit 537ab6a into Kludex:master Jul 3, 2021
@graingert graingert deleted the use-a-context-manager-factory-for-lifespan branch July 3, 2021 17:43
@graingert graingert mentioned this pull request Jul 4, 2021
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When on_shutdown function raises error, starlette doesn't catch it and uvicorn thinks that lifespan is not supported

3 participants