Skip to content

Commit

Permalink
replace copyfileobj with iter_chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
itcarroll committed Jan 7, 2025
1 parent 76a4f59 commit 1515e3e
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions earthaccess/store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
import logging
import shutil
import traceback
from functools import lru_cache
from itertools import chain
Expand Down Expand Up @@ -670,16 +669,13 @@ def _download_file(self, url: str, directory: Path) -> str:
if not path.exists():
try:
session = self.auth.get_session()
with session.get(
url,
stream=True,
allow_redirects=True,
) as r:
with session.get(url, stream=True, allow_redirects=True) as r:
r.raise_for_status()
with open(path, "wb") as f:
# This is to cap memory usage for large files at 1MB per write to disk per thread
# Cap memory usage for large files at 1MB per write to disk per thread
# https://docs.python-requests.org/en/latest/user/quickstart/#raw-response-content
shutil.copyfileobj(r.raw, f, length=1024 * 1024)
for chunk in r.iter_content(chunk_size=1024 * 1024):
f.write(chunk)
except Exception:
logger.exception(f"Error while downloading the file {local_filename}")
raise Exception
Expand Down

0 comments on commit 1515e3e

Please sign in to comment.