小编sku*_*ght的帖子

如何在多线程中使用 tqdm?

我正在尝试使用 tqdm 报告从三个链接下载每个文件的进度,我想使用多线程从每个链接同时下载同时更新进度条。但是当我执行我的脚本时,有多行进度条似乎线程正在同时更新 tqdm 进度条。我在问我应该如何运行多线程来下载文件,同时保持每次下载的进度条,而不会有重复的条填充整个屏幕?这是我的代码。

import os
import sys
import requests
from pathlib import Path
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor as PE


def get_filename(url):
    filename = os.path.basename(url)
    fname, extension = os.path.splitext(filename)
    if extension:
        return filename
    header = requests.head(url).headers
    if "Location" in header:
        return os.path.basename(header["Location"])
    return fname


def get_file_size(url):
    header = requests.head(url).headers
    if "Content-Length" in header and header["Content-Length"] != 0:
        return int(header["Content-Length"])
    elif "Location" in header and "status" not in header:
        redirect_link = header["Location"]
        r = requests.head(redirect_link).headers
        return …
Run Code Online (Sandbox Code Playgroud)

python multithreading tqdm

3
推荐指数
1
解决办法
4148
查看次数

标签 统计

multithreading ×1

python ×1

tqdm ×1