Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Usage without Async Generators #56

Merged
merged 3 commits into from
May 14, 2023

Conversation

ejlangev
Copy link
Contributor

This PR adds a new option that runs the code that generates responses in the same TaskGroup that is used for streaming the response. Doing so avoids the need for an async generator or some other form of persistent iterator that exists across requests.

The motivation for doing that is that async generators have less well defined error and cleanup behavior especially when they are interrupted see here.

In this new implementation, the coroutine that is generating outputs can be straightforwardly cancelled just like all the others in response to disconnects or server shutdowns and will be able to run its cleanup code right then and there which makes it easier to understand what is going to happen in all cases and importantly to propagate all exceptions where they should go.

This is conceptually similar to what is being done in the stream generator examples except it works within a single endpoint and doesn't require any shared state.

@sysid
Copy link
Owner

sysid commented May 14, 2023

Pretty cool.
Thanks @ejlangev !

@sysid sysid merged commit 97eb623 into sysid:master May 14, 2023
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.

2 participants