From 78377fa6d72d6932cc51c57726cf3496320f622a Mon Sep 17 00:00:00 2001 From: xiexianbin Date: Sun, 7 Sep 2025 22:22:53 +0800 Subject: [PATCH] fix: httpx.Client is slow when sending large data This commit addresses an issue where sending large data with httpx.Client was extremely slow. The problem was that the entire data payload was being loaded into memory and sent in a single operation. This fix refactors the sending process for iterable bytes types to use a chunked approach with `yield`, which significantly improves performance and reduces memory usage for large payloads. Signed-off-by: xiexianbin --- httpx/_content.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/httpx/_content.py b/httpx/_content.py index 6f479a0885..f4acfc4045 100644 --- a/httpx/_content.py +++ b/httpx/_content.py @@ -61,7 +61,11 @@ def __iter__(self) -> Iterator[bytes]: else: # Otherwise iterate. for part in self._stream: - yield part + # For bytes types, should use yield to send data in + # chunks. Sending a very large part at once will + # result in extremely slow transmission. + for i in range(0, len(part), self.CHUNK_SIZE): + yield part[i : i + self.CHUNK_SIZE] class AsyncIteratorByteStream(AsyncByteStream):