Skip to content

Commit

Permalink
Avoid loading entire files into memory when downloading from ESGF (#2434
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bouweandela authored and chrisbillowsMO committed Jun 12, 2024
1 parent 20ae705 commit 5145348
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion esmvalcore/esgf/_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,10 @@ def _download(self, local_file, url):
cert=get_credentials())
response.raise_for_status()
with tmp_file.open("wb") as file:
for chunk in response.iter_content(chunk_size=None):
# Specify chunk_size to avoid
# https://github.com/psf/requests/issues/5536
megabyte = 2**20
for chunk in response.iter_content(chunk_size=megabyte):
if hasher is not None:
hasher.update(chunk)
file.write(chunk)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/esgf/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def test_single_download(mocker, tmp_path, checksum):
# We checked for a valid response
response.raise_for_status.assert_called_once()
# And requested a reasonable chunk size
response.iter_content.assert_called_with(chunk_size=None)
response.iter_content.assert_called_with(chunk_size=2**20)


def test_download_skip_existing(tmp_path, caplog):
Expand Down

0 comments on commit 5145348

Please sign in to comment.