Skip to content

Commit a5224d4

Browse files
committed
[Fix] Stop writing to stdout when downloading (#4237)
* Stop writing to stdout when downloading * Fix bug
1 parent 53b0272 commit a5224d4

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

paddlex/inference/serving/basic_serving/_app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ async def close(self):
114114
if not self._closed:
115115
self._queue.put(None)
116116
await call_async(self._thread.join)
117+
self._closed = True
117118

118119
def _worker(self):
119120
while not self._closed:

paddlex/utils/download.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ def print(self, str_, end=False):
3939
str_ += "\n"
4040
self._last_time = 0
4141
if time.time() - self._last_time >= self._flush_intvl:
42-
sys.stdout.write(f"\r{str_}")
42+
sys.stderr.write(f"\r{str_}")
4343
self._last_time = time.time()
44-
sys.stdout.flush()
44+
sys.stderr.flush()
4545

4646

4747
def _download(url, save_path, print_progress):
4848
if print_progress:
49-
print(f"Connecting to {url} ...")
49+
print(f"Connecting to {url} ...", file=sys.stderr)
5050

5151
with requests.get(url, stream=True, timeout=15) as r:
5252
r.raise_for_status()
@@ -62,7 +62,10 @@ def _download(url, save_path, print_progress):
6262
total_length = int(total_length)
6363
if print_progress:
6464
printer = _ProgressPrinter()
65-
print(f"Downloading {os.path.basename(save_path)} ...")
65+
print(
66+
f"Downloading {os.path.basename(save_path)} ...",
67+
file=sys.stderr,
68+
)
6669
for data in r.iter_content(chunk_size=4096):
6770
dl += len(data)
6871
f.write(data)
@@ -95,17 +98,17 @@ def _extract_tar_file(file_path, extd_dir):
9598
try:
9699
f.extract(file, extd_dir)
97100
except KeyError:
98-
print(f"File {file} not found in the archive.")
101+
print(f"File {file} not found in the archive.", file=sys.stderr)
99102
yield total_num, index
100103
except Exception as e:
101-
print(f"An error occurred: {e}")
104+
print(f"An error occurred: {e}", file=sys.stderr)
102105

103106

104107
def _extract(file_path, extd_dir, print_progress):
105108
"""extract"""
106109
if print_progress:
107110
printer = _ProgressPrinter()
108-
print(f"Extracting {os.path.basename(file_path)}")
111+
print(f"Extracting {os.path.basename(file_path)}", file=sys.stderr)
109112

110113
if zipfile.is_zipfile(file_path):
111114
handler = _extract_zip_file

0 commit comments

Comments
 (0)