-
-
Notifications
You must be signed in to change notification settings - Fork 27
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
Revisit the "optional nursery" pattern #31
Comments
After fiddling with some different API designs (and thinking about how to close a server—see #7), I decided that a context manager doesn't make sense for
I think the ideal server API looks like this:
To end the server, the caller must use regular Trio cancellation, e.g. New issue scope:
|
* New listen_in_nursery() API for server. * Add unit tests for implicit vs explicit nursery. * Update example server.
This is more consistent with the Trio API.
After getting feedback on PR #34 from Nathaniel and John (both in the code review and also on Gitter), I am going to reject that PR and start over. I changed the title of this ticket to indicate that the "optional nursery" pattern needs to be revisited in the client (#20) and server. Client nurseryThe purpose of the "optional nursery" pattern is to simplify cases with lots of connections (i.e. deep nesting of context managers) or where connections are added and removed dynamically. Therefore, the context manager that I implemented in #28 missed the mark, because even though it has an optional The API needs to have two functions: a context manager that creates an internal nursery, and a a regular Server nurseryUnlike the client, the server doesn't have much need for the "optional nursery" pattern, because the server doesn't need to be launched in its own While we're at it, each connection should be launched inside of a new nursery as well. I.e., the nursery tree for two connections might look like this:
This makes it easy to cancel the tasks associated with a single connection without affecting other connections. |
In addition to open_websocket and open_websocket_url, which are both context managers that do not have a nursery argument, I have added connect_websocket() and connect_websocket_url(), which take a nursery argument and are not context managers.
This is passed down to serve_tcp(). Add tests to cover this feature.
This is done:
It turned out that the server already creates a new nursery for each connection, so no changes there. |
The client API was recently changed from a class to an async context manager function and the nursery argument was made optional. The server API should offer similar features:
serve_websocket(…)
async context manager to mirror the client'sopen_websocket(…)
.WebSocketServer
instance.nursery
an optional argument toserve_websocket()
The text was updated successfully, but these errors were encountered: