You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
many network protocols are multiplexed by nature, i.e. multiple logical connections share one physical connection. Examples include http2 (multiple requests in parallel), ssh (e.g. port forwardings), irc (chat with multiple persons).
Some background task is required which reads data from the physical connection and distributes it to the handler of the logical connection.
I'd like to see an easy way to create code like this:
asyncwithHttp2Client("server.example.net") ashttp:
asyncwithtrio.open_nursery() asnursery:
forpathin {'/foo', '/bar', '/baz'}:
nursery.start_soon(http.get, path) # GET result is unused, for sake of brevity.
The Http2Client context manager would create a nursery and start a background task which reads data from the http server and handles it somehow.
The get method would send a request over the multiplexed connection and wait for the reply. The background task reads a reply from the server and sees e.g. "It's for the baz request" and then delivers it to the http.get foreground task for baz which will then return the result to the user.
As soon as the body of the context manager finishes, the background job is notified. In above example, it would cleanly shut down the http2 and TLS connections.
I'm not sure whether it makes sense for Trio to include some kind of helpers for this pattern, or not. Mostly I'm just not sure what those helpers would look like, or if it's possible to make ones that are useful and general enough to include :-). A basic implementation is just like:
Activity
belm0 commentedon Jul 8, 2019
See also #467
njsmith commentedon Jul 8, 2019
Yeah, this is definitely a common pattern. trio-websocket is a fairly mature example, and some relevant links are:
I'm not sure whether it makes sense for Trio to include some kind of helpers for this pattern, or not. Mostly I'm just not sure what those helpers would look like, or if it's possible to make ones that are useful and general enough to include :-). A basic implementation is just like:
That's short, and has a pretty high-density of protocol-specific stuff.
oremanj commentedon May 12, 2020
I think this is redundant with #467 in terms of next-actions, so closing.