Connections apparently not reused when using stream() #989
Replies: 1 comment
|
The context manager itself isn't what prevents HTTP/1.1 reuse. The important distinction is whether the response body has been consumed before the context exits.
That explains the difference from response = await self.handle_async_request(request)
try:
await response.aread()
finally:
await response.aclose()whereas Consume the stream inside the context: async with http_pool.stream(
method="GET",
url="http://github.com/",
) as response:
async for chunk in response.aiter_stream():
# process chunk
...If the entire body is consumed normally, the HTTP/1.1 response reaches its completed state and the pool can reuse the connection. If you intentionally stop reading early, closing the connection is expected for a response whose remaining HTTP/1.1 body can't safely be left on the socket for the next request. I checked the current implementation: |
Uh oh!
There was an error while loading. Please reload this page.
In this example:
I would expect the output to show one AsyncHTTPConnection with Request Count: 2 being used. However, I see the following:
indicating the context manager closes the connection in between.
With
http2=True, http1=FalseI get the expected connection reuse:With regular
request(), notstream():I get the connection reuse I expect, but I would like to stream both request and response body.
Maybe I simply misunderstand the purpose of the context manager of
streamThanks
All reactions