Refactoring... context managed timeouts. #982
tomchristie
started this conversation in
General
Replies: 1 comment
-
It should probably be possible to cancel the timeout or change the deadline from another thread by using https://github.com/python-trio/trio/wiki/notes-to-self#blocking-read-hackpy |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Prompted by review @karpetrosyan's work on #936.
I think there's some preceding work we could do here to improve timeouts in
httpcore
so that we've got an easier-to-work-with set of primitives. I think we probably want to tackle this refactoring before dealing with introducing overall timeouts, since it deals with the underlying issue.Here's where we currently are...
We need a consistent approach to timeouts across both sync and async, which is currently awkward because the existing primitives in each case are different. Standard threaded codebases use
.set_timeout
on the socket. Async codebases use context managed timeout blocks, such astrio.fail_after()
.Currently we mediate between these two differing styles by ensuring that all socket operations take a
timeout=...
parameter. Within the async backends we use.fail_after()
within these operations. This ensures that we've got an API that's consistent across both sync & async cases...sync...
async...
Here's where I think we should be...
The thing is,
trio
gets the design of timeouts right. Timeouts as context managed blocks is easier to reason about, and allows us to handle cases such as "overall" timeouts in an obvious way.Really we'd like to have the alternate style here, all the way through our stack.
sync...
async...
We should be aiming to drop the
timeout
parameter from theNetworkStream
API, and have context-managed timeouts that are dealt with outside of that layer.An example to demonstrate...
Here's a little sync API with context managed timeouts...
Using the API then ~looks like this...
How we can apply this in HTTP core...
NetworkBackend
classes implement atimeout
context manager, rather than having atimeout=...
parameter on the socket operations.Beta Was this translation helpful? Give feedback.
All reactions