how to handle early closing a stream on client side? (stream is never cancelled by the server but should support closing from client side) #2550
-
|
I want to use httpx's streaming feature to interact with a service that provides a long-lived event stream over http2. Currently I'm only needing to support the http-core sync backend. The general shape of the code is that the program initiates an httpx stream() and then a handler_method will be triggered for every decoded response from the stream. Within the handler_method, the client may decide to shutdown the stream early -- which leads to a call of the There are two issues that I'm having currently having which I'm working around by copy/pasting some code from
Here's the code I've copy/pasted and edited within my codebase -- it looks as similar as possible to upstream and my hope is that it provides readable context for understanding my issues ... I may not be understanding the library correctly and would definitely appreciate pointers if there's a better way get behavior like the above ... Or if not, it would be great if httpx could be modified to better support this use case! Many thanks for any help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
I think I figured out how to do what I want -- and this appears to suffice for my current use cases without any of the hax like the above being needed ... In my case, I could simply adjust the outermost loop consuming and transorming/processing the |
Beta Was this translation helpful? Give feedback.
I think I figured out how to do what I want -- and this appears to suffice for my current use cases without any of the hax like the above being needed ...
In my case, I could simply adjust the outermost loop consuming and transorming/processing the
stream_response.iter_bytes()to simplybreakout of that loop when an early exit state has been identified. That can be done without anyone callingclose()on any of the Client/Response objects. The stream and connection can then go through the normal shutdown logic when the context manager scope has ended and nothing will ever bubble up into my code ...