Replies: 1 comment
|
When you stream an HTTPX response using 1. What happens during an errorIf the remote server disconnects, drops the connection, times out, or sends malformed chunks mid-stream:
2. How to handle it cleanly with try/exceptWrap the stream context and the chunk consumption inside the import httpx
try:
with httpx.Client(timeout=30.0) as client:
with client.stream("GET", "https://example.com/large-file") as response:
response.raise_for_status()
for chunk in response.iter_bytes(chunk_size=8192):
# Process the chunk (e.g., write to disk or pipe)
pass
except httpx.ReadTimeout:
print("Network stalled: reading next chunk timed out mid-stream.")
except httpx.RemoteProtocolError as exc:
print(f"Connection closed prematurely or protocol error: {exc}")
except httpx.HTTPError as exc:
print(f"HTTPX error encountered during stream: {exc}")3. Key exceptions to watch for during streaming:
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
if I'm using iter_bytes inside try exception, what happens typical errors happens mid stream?
All reactions