Skip to content

Commit da46c21

Browse files
authored
More robust stream to stdout in hf cp command (#3968)
* More robust stream to stdout in hf cp command * read by chunks of 32MB * style
1 parent e41dce2 commit da46c21

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

src/huggingface_hub/cli/buckets.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import json
1818
import os
1919
import sys
20-
import tempfile
2120
from datetime import datetime
2221
from typing import Annotated, Optional, Union
2322

@@ -33,7 +32,13 @@
3332
_parse_bucket_path,
3433
_split_bucket_id_and_prefix,
3534
)
36-
from huggingface_hub.utils import StatusLine, are_progress_bars_disabled, disable_progress_bars, enable_progress_bars
35+
from huggingface_hub.utils import (
36+
SoftTemporaryDirectory,
37+
StatusLine,
38+
are_progress_bars_disabled,
39+
disable_progress_bars,
40+
enable_progress_bars,
41+
)
3742

3843
from ._cli_utils import (
3944
FormatOpt,
@@ -981,11 +986,12 @@ def cp(
981986
if not pbar_was_disabled:
982987
disable_progress_bars()
983988
try:
984-
with tempfile.TemporaryDirectory() as tmp_dir:
989+
with SoftTemporaryDirectory() as tmp_dir:
985990
tmp_path = os.path.join(tmp_dir, filename)
986991
api.download_bucket_files(bucket_id, [(prefix, tmp_path)])
987992
with open(tmp_path, "rb") as f:
988-
sys.stdout.buffer.write(f.read())
993+
while chunk := f.read(32_000_000): # 32MB chunks
994+
sys.stdout.buffer.write(chunk)
989995
finally:
990996
if not pbar_was_disabled:
991997
enable_progress_bars()

0 commit comments

Comments
 (0)