#4272 Replace iter_content with copyfileobj
Opened 3 months ago by tkopecek. Modified 3 months ago
tkopecek/koji download-speed  into  master

file modified
+2 -2
@@ -38,6 +38,7 @@ 

  import pwd

  import random

  import re

+ import shutil

  import socket

  import struct

  import sys
@@ -2028,8 +2029,7 @@ 

  

      resp = request_with_retry().get(url, stream=True)

      try:

-         for chunk in resp.iter_content(chunk_size=8192):

-             fo.write(chunk)

+         shutil.copyfileobj(resp.raw, fo)

      finally:

          resp.close()

      resp.raise_for_status()

It is a bit faster. With a local server it gives 70% of original time.
For real network the gain would be smaller, but doesn't hurt to have it
there.

I'm curious why one is faster, and whether that will be true across different versions of python and requests

I suspect that at least buffer is much larger (8k vs 64k), but I've not tested the former with 64k buffer.

Metadata