caching_proxy.py: add comment about not using shutil.copyfileobj()

This commit is contained in:
Johannes Schauer Marin Rodrigues 2023-06-14 07:34:51 +02:00
parent 910427b598
commit 2b327ac0fd
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -57,6 +57,8 @@ class ProxyRequestHandler(http.server.BaseHTTPRequestHandler):
self.send_header("Content-Length", oldpath.stat().st_size) self.send_header("Content-Length", oldpath.stat().st_size)
self.end_headers() self.end_headers()
with oldpath.open(mode="rb") as old, newpath.open(mode="wb") as new: with oldpath.open(mode="rb") as old, newpath.open(mode="wb") as new:
# we are not using shutil.copyfileobj() because we want to
# write to two file objects simultaneously
while True: while True:
buf = old.read(64 * 1024) # same as shutil uses buf = old.read(64 * 1024) # same as shutil uses
if not buf: if not buf:
@ -81,6 +83,9 @@ class ProxyRequestHandler(http.server.BaseHTTPRequestHandler):
self.send_header(k, v) self.send_header(k, v)
self.end_headers() self.end_headers()
with newpath.open(mode="wb") as f: with newpath.open(mode="wb") as f:
# we are not using shutil.copyfileobj() because we want to
# write to two file objects simultaneously and throttle the
# writing speed to 1024 kB/s
while True: while True:
buf = res.read(64 * 1024) # same as shutil uses buf = res.read(64 * 1024) # same as shutil uses
if not buf: if not buf: