Repository navigation
Server api - #34
Server api#34mehaase wants to merge 2 commits into
Conversation
* 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.
| ssl_context=ssl_context) | ||
| await server.listen() | ||
| server = WebSocketServer(handler, args.host, args.port, ssl_context) | ||
| async with trio.open_nursery() as nursery: |
There was a problem hiding this comment.
these three lines are the equivalent of await server.listen()
I realize that uses may commonly have some siblings to the websocket server, but suggest keeping example code idiomatic.
| Listen for incoming connections. | ||
|
|
||
| The listener and all connection tasks are spawned in an internal | ||
| nursery. This coroutine runs until cancelled. |
There was a problem hiding this comment.
I believe trio lingo is "task". In trio style, coroutines are considered a detail of Python async and not passed around. Await is use immediately on any coroutine.
There was a problem hiding this comment.
It's true we avoid the word "coroutine" as being jargony and ambiguous. Here I'd just say "function", or "async function" if i wanted to emphasize that its call signature has an await. I'd use "task" if I wanted to emphasize it running as a separate "thread" of execution – so it's not a task until someone calls it via nursery.start(_soon).
|
|
||
| :param task_status: part of Trio nursery protocol | ||
| ''' | ||
| async with trio.open_nursery() as nursery: |
There was a problem hiding this comment.
It seems wasteful to have this extra container due to the implementation. More a matter of noise than efficiency-- consider tools that inspect task hierarchy, etc. In the client case, isn't there a method with optional nursery arg and implement each case minimally?
I'm still biased and not convinced the "optional nursery" pattern should be a thing. In fact, is the goal of that to just avoid a nursery level as well? If you remove listen-in-nursery the API is equally capable.
There was a problem hiding this comment.
It seems wasteful to have this extra container due to the implementation.
I'm not sure what you mean by "extra container"? The previous listen() method also opens a nursery, so the total number of nurseries is the same in this code path. If you call listen_in_nursery() directly, then you'll end up with one less nursery.
In the client case, isn't there a method with optional nursery arg and implement each case minimally?
Yes, the client has an optional nursery arg, but it's implemented the same way: there is a connect_in_nursery(…) function that is either passed the caller's nursery or a newly created nursery.
For consistency, though, it seems like listen() should have an optional nursery argument—just like the client does—and listen_in_nursery() should not be part of the API.
I'm still biased and not convinced the "optional nursery" pattern should be a thing.
To be honest, I'm not sure what the purpose of the optional nursery is either. I added it because Nathaniel recommended it! I'll start a chat about this topic on Gitter.
There was a problem hiding this comment.
So there's some argument for having a kind of "optional nursery" for both the client and server, but the reasons and patterns are different.
For the client, if you don't let the user pass in a nursery, then they have to use an async with block, and that means every connection is forced to be bound to a specific task, and it's inconvenient to have too many connections bound to the same task. (Like, how many levels of indentation can you really use?) In many cases these limitations are fine and just using async with is the easiest way to go, but I believe there will be advanced use cases where these limitations become a problem, and to support those it's important to have the option of using an explicit nursery, to let users manually decouple the lifetime of the connection from the stack frame that creates the connection.
For the server, this doesn't really apply the same way: the server isn't an object that you interact with and that might get passed between tasks, it's a function that runs until it's cancelled. So I don't think the "optional nursery" here is really doing anything useful... like, with the current code, await server.listen_in_nursery(nursery) is essentially the same as await nursery.start(server.listen), right? So it seems to violate "there should be one, and preferably only one, obvious way to do it".
However, Trio's serve_* functions do take an optional nursery argument. It's something slightly different though: it allows you to split up the listening-for-new-connections tasks and the handle-ongoing-connection tasks into separate nurseries. I'm not 100% sure this is actually useful, but for example it makes it possible to cancel one without cancelling the other. So maybe we want a listen that takes an optional nursery argument, which it passes down to serve_tcp?
|
Thanks for feedback. I'm going to close this and try again in a new PR. I've updated #31 accordingly. |
Modifying server API as described in #31. I think this is uncontroversial but @belm0 if you want to take a look, feedback is always appreciated.