如何将 tqdm 与多线程结合使用?

2023-11-27

我试图使用 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 int(r["Content-Length"])


def download_file(url, filename=None):
    # Download to the Downloads folder in user's home folder.
    download_dir = os.path.join(Path.home(), "Downloads")
    if not os.path.exists(download_dir):
        os.makedirs(download_dir, exist_ok=True)
    if not filename:
        filename = get_filename(url)
    file_size = get_file_size(url)
    abs_path = os.path.join(download_dir, filename)
    chunk_size = 1024
    with open(abs_path, "wb") as f, requests.get(url, stream=True) as r, tqdm(
            unit="B",
            unit_scale=True,
            unit_divisor=chunk_size,
            desc=filename,
            total=file_size,
            file=sys.stdout
    ) as progress:
        for chunk in r.iter_content(chunk_size=chunk_size):
            data = f.write(chunk)
            progress.update(data)


if __name__ == "__main__":
    urls = ["http://mirrors.evowise.com/linuxmint/stable/20/linuxmint-20-xfce-64bit.iso",
            "https://www.vmware.com/go/getworkstation-win",
            "https://download.geany.org/geany-1.36_setup.exe"]
    with PE(max_workers=len(urls)) as ex:
        ex.map(download_file, urls)

我稍微修改了我的代码,这是我从将 tqdm 与并发.futures 一起使用吗?.

    def download_file(url, filename=None):
    # Download to the Downloads folder in user's home folder.
    download_dir = os.path.join(Path.home(), "Downloads")
    if not os.path.exists(download_dir):
        os.makedirs(download_dir, exist_ok=True)
    if not filename:
        filename = get_filename(url)
    # file_size = get_file_size(url)
    abs_path = os.path.join(download_dir, filename)
    chunk_size = 1024
    with open(abs_path, "wb") as f, requests.get(url, stream=True) as r:
        for chunk in r.iter_content(chunk_size=chunk_size):
            f.write(chunk)


if __name__ == "__main__":
    urls = ["http://mirrors.evowise.com/linuxmint/stable/20/linuxmint-20-xfce-64bit.iso",
            "https://www.vmware.com/go/getworkstation-win",
            "https://download.geany.org/geany-1.36_setup.exe"]
    with PE() as ex:
        for url in urls:
            tqdm(ex.submit(download_file, url),
                 total=get_file_size(url),
                 unit="B",
                 unit_scale=True,
                 unit_divisor=1024,
                 desc=get_filename(url),
                 file=sys.stdout)

但在我修改代码后,该栏没有更新......

My problem:
tqdm has duplicated progress bars

I have no problem with concurrent download, but has problem implementing tqdm to update individual progress for each link, below is what I want to achieve:
ideally should have progress bar for each download.

我使用了其中一种解决方案:

if __name__ == "__main__":
urls = ["http://mirrors.evowise.com/linuxmint/stable/20/linuxmint-20-xfce-64bit.iso",
        "https://www.vmware.com/go/getworkstation-win",
        "https://download.geany.org/geany-1.36_setup.exe"]

with tqdm(total=len(urls)) as pbar:
    with ThreadPoolExecutor() as ex:
        futures = [ex.submit(download_file, url) for url in urls]
        for future in as_completed(futures):
            result = future.result()
            pbar.update(1)

But this is the result: enter image description here


这是总体思路(按照您的意愿格式化):

from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm
import requests


def download_file(url):
    with requests.get(url, stream=True) as r:
        for chunk in r.iter_content(chunk_size=50000):
            pass
    return url


if __name__ == "__main__":
    urls = ["http://mirrors.evowise.com/linuxmint/stable/20/linuxmint-20-xfce-64bit.iso",
            "https://www.vmware.com/go/getworkstation-win",
            "https://download.geany.org/geany-1.36_setup.exe"]

    with tqdm(total=len(urls)) as pbar:
        with ThreadPoolExecutor(max_workers=len(urls)) as ex:
            futures = [ex.submit(download_file, url) for url in urls]
            for future in as_completed(futures):
                result = future.result()
                pbar.update(1)

如果您知道每次下载的长度,请进行模拟

from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm
import requests
import time
import random


def download_file(url, pbar):
    for _ in range(30):
        time.sleep(.50 * random.random())
        pbar.update(1)
    return url


if __name__ == "__main__":
    urls = ["http://mirrors.evowise.com/linuxmint/stable/20/linuxmint-20-xfce-64bit.iso",
            "https://www.vmware.com/go/getworkstation-win",
            "https://download.geany.org/geany-1.36_setup.exe"]

    with tqdm(total=90) as pbar:
        with ThreadPoolExecutor(max_workers=3) as ex:
            futures = [ex.submit(download_file, url, pbar) for url in urls]
            for future in as_completed(futures):
                result = future.result()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 tqdm 与多线程结合使用? 的相关文章

  • 为什么从 Pandas 1.0 中删除了日期时间?

    我在 pandas 中处理大量数据分析并每天使用 pandas datetime 最近我收到警告 FutureWarning pandas datetime 类已弃用 并将在未来版本中从 pandas 中删除 改为从 datetime 模块
  • 与区域指示符字符类匹配的 python 正则表达式

    我在 Mac 上使用 python 2 7 10 表情符号中的标志由一对表示区域指示符号 https en wikipedia org wiki Regional Indicator Symbol 我想编写一个 python 正则表达式来在
  • Python 中的哈希映射

    我想用Python实现HashMap 我想请求用户输入 根据他的输入 我从 HashMap 中检索一些信息 如果用户输入HashMap的某个键 我想检索相应的值 如何在 Python 中实现此功能 HashMap
  • 定期更新 SWT 会导致 GUI 冻结

    Problem 当 GUI 字段定期更新时 SWT 会冻结 我想要一个基于 SWT 的 GUI 其中文本字段的值会定期递增 最初我从单独的线程访问 textField 导致抛出异常 线程 Thread 0 org eclipse swt S
  • 使用 Python 从文本中删除非英语单词

    我正在 python 上进行数据清理练习 我正在清理的文本包含我想删除的意大利语单词 我一直在网上搜索是否可以使用像 nltk 这样的工具包在 Python 上执行此操作 例如给出一些文本 Io andiamo to the beach w
  • datetime.datetime.now() 返回旧值

    我正在通过匹配日期查找 python 中的数据存储条目 我想要的是每天选择 今天 的条目 但由于某种原因 当我将代码上传到 gae 服务器时 它只能工作一天 第二天它仍然返回相同的值 例如当我上传代码并在 07 01 2014 执行它时 它
  • 如何使用 Mysql Python 连接器检索二进制数据?

    如果我在 MySQL 中创建一个包含二进制数据的简单表 CREATE TABLE foo bar binary 4 INSERT INTO foo bar VALUES UNHEX de12 然后尝试使用 MySQL Connector P
  • 在 Sphinx 文档中*仅*显示文档字符串?

    Sphinx有一个功能叫做automethod从方法的文档字符串中提取文档并将其嵌入到文档中 但它不仅嵌入了文档字符串 还嵌入了方法签名 名称 参数 我如何嵌入only文档字符串 不包括方法签名 ref http www sphinx do
  • javascript 是否有等效的 __repr__ ?

    我最接近Python的东西repr这是 function User name password this name name this password password User prototype toString function r
  • Python3 在 DirectX 游戏中移动鼠标

    我正在尝试构建一个在 DirectX 游戏中执行一些操作的脚本 除了移动鼠标之外 我一切都正常 是否有任何可用的模块可以移动鼠标 适用于 Windows python 3 Thanks I used pynput https pypi or
  • 从 NumPy ndarray 中选择行

    我只想从 a 中选择某些行NumPy http en wikipedia org wiki NumPy基于第二列中的值的数组 例如 此测试数组的第二列包含从 1 到 10 的整数 gt gt gt test numpy array nump
  • 如何使用原始 SQL 查询实现搜索功能

    我正在创建一个由 CS50 的网络系列指导的应用程序 这要求我仅使用原始 SQL 查询而不是 ORM 我正在尝试创建一个搜索功能 用户可以在其中查找存储在数据库中的书籍列表 我希望他们能够查询 书籍 表中的 ISBN 标题 作者列 目前 它
  • Pandas 将多行列数据帧转换为单行多列数据帧

    我的数据框如下 code df Car measurements Before After amb temp 30 268212 26 627491 engine temp 41 812730 39 254255 engine eff 15
  • 如何解决 PDFBox 没有 unicode 映射错误?

    我有一个现有的 PDF 文件 我想使用 python 脚本将其转换为 Excel 文件 目前正在使用PDFBox 但是存在多个类似以下错误 org apache pdfbox pdmodel font PDType0Font toUnico
  • python import inside函数隐藏现有变量

    我在我正在处理的多子模块项目中遇到了一个奇怪的 UnboundLocalError 分配之前引用的局部变量 问题 并将其精简为这个片段 使用标准库中的日志记录模块 import logging def foo logging info fo
  • Python ImportError:无法导入名称 __init__.py

    我收到此错误 ImportError cannot import name life table from cdc life tables C Users tony OneDrive Documents Retirement retirem
  • 使用for循环时如何获取前一个元素? [复制]

    这个问题在这里已经有答案了 可能的重复 Python 循环内的上一个和下一个值 https stackoverflow com questions 1011938 python previous and next values inside
  • Scipy Sparse:SciPy/NumPy 更新后出现奇异矩阵警告

    我的问题是由大型电阻器系统的节点分析产生的 我基本上是在设置一个大的稀疏矩阵A 我的解向量b 我正在尝试求解线性方程A x b 为了做到这一点 我正在使用scipy sparse linalg spsolve method 直到最近 一切都
  • 更改 Tk 标签小部件中单个单词的颜色

    我想更改 Tkinter 标签小部件中单个单词的字体颜色 我知道可以使用文本小部件来实现与我想要完成的类似的事情 例如使单词 YELLOW 显示为黄色 self text tag config tag yel fg clr yellow s
  • 使用 z = f(x, y) 形式的 B 样条方法来拟合 z = f(x)

    作为一个潜在的解决方案这个问题 https stackoverflow com questions 76476327 how to avoid creating many binary switching variables in gekk

随机推荐

  • Java中的异常与继承

    假设我们有这个问题 public class Father public void method1 public class Child1 extends Father public void method1 throws Exceptio
  • 替换 contenteditable div 中选定的文本

    我一直在寻找答案 但失败了 是否有跨浏览器解决方案来替换所选文本内容可编辑 div 我只是希望用户突出显示一些文本并将突出显示的文本替换为xxxxx 以下内容将在所有主要浏览器中完成这项工作 function replaceSelected
  • 存储 ENUM 值的 PostgreSQL 数组

    我有一个可以有状态的表 statuses unmoderated nominee finalist winner status db Enum statuses name enum nomination status metadata db
  • 如何使用Web API限制DOS攻击

    我计划使用 MVC4 和 Web APi 开发一个网站 它是一个简单的应用程序 将根据搜索显示客户信息 对于搜索功能 我使用 Ajax get 方法调用 webApi 我知道我应该使用 Post 但考虑这是当前的实现 我的 API 调用是
  • JOIN ON 子句中的 T-SQL Case 语句

    我正在尝试在 a 中构建一个 case if 语句JOIN ON clause LEFT JOIN CTSTRC Statuses ON RIGHT Statuses STRID 3 CTE F61 问题是该列 Statuses STRID
  • PHP dirname 返回符号链接路径

    假设我有一个符号链接 one directory to two directory If I echo dirname dirname FILE 它返回 one directory 最好的退货方式是什么 two directory 用法示例
  • 不使用 nightly 时如何忽略基准测试?

    我有一个包含一些基准测试和测试的文件 想针对稳定版 测试版和夜间版进行测试 然而 要么我不使用基准测试 要么稳定 测试版抱怨 使用 stable beta 时有没有办法隐藏所有基准部分 作为示例 以下代码来自book feature tes
  • 如何在 Windows 上从 pygraphviz 运行 neato

    我正在尝试在 python v 2 7 中使用 pygraphviz 和 networkx 来创建网络映射 我在 stackoverflow 上发现了一个看起来非常有用的脚本 import networkx as nx import num
  • GDB TUI - 输出未对齐

    我在ubuntu上使用gdb 7 7 1 GNU gdb Ubuntu 7 7 1 0ubuntu5 14 04 2 7 7 1 我的终端是 Konsole 2 13 2 我遇到的问题是 当我进入 TUI 模式时 在一两个调试器会话之后 会
  • 设置请求持续时间的全局变量

    我可以为单个请求的长度设置某种全局变量 以便页面的所有控件都可以响应它 而不必将其传递给每个控件吗 例如 如果有人点击我的 MasterPage 上的 保存 按钮 我是否可以设置一些内容 以便我的页面上的每个 UserControl 都可以
  • Future.wait() 不能在没有纤程的情况下等待(在 Meteor.method 中等待另一个 future 时)

    In Meteor 我正在编写一个方法 该方法必须检查某个路径的子目录中是否有新文件 我首先想列出其中的子目录Meteor之后我child process exec一个简单的 bash 脚本 列出自上次执行以来添加的文件 我在使目录发现异步
  • 使用 O 表示法在 for 循环中对 LinkedList 调用 get() 的复杂性

    我有一个 uni 实用程序 可以使用 O 表示法确定一小部分代码的复杂性 代码是 for int i 0 i lt list size i System out println list get i 所讨论的列表是一个链接列表 对于我们的实
  • 如何破译 C 中复杂的指针声明?

    所以我想举个例子 int pi pi is a pointer that points to an integer const int cpi cpi is a pointer that points to a constant integ
  • Excel - 如何转换 UTC 日期时间

    我想在Excel中使用这个字符串作为日期时间 2016 04 06T18 05 32 6550717 03 00 怎样才能转换呢 日期 时间中有两件事引起了问题 第一个是T这表示时间的开始 Excel 使用空格 第二个是右侧的所有内容 如果
  • TestNG 中测试的执行顺序

    如何自定义TestNG中测试的执行顺序 例如 public class Test1 Test public void test1 System out println test1 Test public void test2 System
  • 如果“z”中有“x”和“y”:

    我正在用 python 编写一个问答脚本 它获取 raw input 并将其设置为 theQuestion 我试过if var1 and var2 in theQuestion 但它会查找其中一个字符串 而不是同时查找两个字符串 有没有一种
  • 使用 LongListSelector 连续分页

    当我的 LongListSelector 滚动到底部时 我想自动从 Web 服务加载更多数据 就像商店应用程序一样 我的问题是我找不到任何事件来触发加载更多操作 Microsoft 的建议是使用 LongListSelector ItemR
  • 如何在 Ionic 2 中的选择组件内使用图像

    我正在尝试将图像放入SelectIonic 2 中的组件 我已将图像源文件放入www img我的 Ionic 2 项目中的文件夹 然而 使用一个简单的img tag 使用此代码不显示任何图像
  • goto 可以打破嵌套循环吗?

    JavaScript 支持类似于 goto 的语法来打破嵌套循环 总的来说 这不是一个好主意 但它被认为是可以接受的做法 C 不直接支持break labelName语法 但它确实支持臭名昭著的goto 我相信 C 中可以实现等效的功能 i
  • 如何将 tqdm 与多线程结合使用?

    我试图使用 tqdm 报告从三个链接下载每个文件的进度 我想使用多线程从每个链接同时下载 同时更新进度条 但是当我执行脚本时 有多行进度条 似乎线程正在同时更新 tqdm 进度条 我问我应该如何运行多线程来下载文件 同时保持每次下载的进度条