使用 tqdm 的不同行为

Different behavior using tqdm

我正在为一个网站做一个图片下载项目,但我在使用 tqdm 时遇到了一些奇怪的行为。在下面的代码中,我包含了两个用于制作 tqdm 进度条的选项。在选项一中,我没有将响应中的可迭代内容直接传递到 tqdm,而在第二个选项中我做了。虽然代码看起来很相似,但结果却大相径庭。

This is what the progress bar's result looks like using Option 1

This is what the progress bar's result looks like using Option 2

选项一是我想要的结果,但我找不到对使用选项 2 的行为的解释。谁能帮我解释一下这个行为?

import requests
from tqdm import tqdm
import os

# Folder to store in
default_path = "D:\Downloads"


def download_image(url):
    """

    This function will download the given url's image with proper filename labeling
    If a path is not provided the image will be downloaded to the Downloads folder
    """

    # Establish a Session with cookies
    s = requests.Session()

    # Fix for pixiv's request you have to add referer in order to download images
    response = s.get(url, headers={'User-Agent': 'Mozilla/5.0',
                                   'referer': 'https://www.pixiv.net/'}, stream=True)

    file_name = url.split("/")[-1]  # Retrieve the file name of the link
    together = os.path.join(default_path, file_name)  # Join together path with the file_name. Where to store the file
    file_size = int(response.headers["Content-Length"])  # Get the total byte size of the file
    chunk_size = 1024  # Consuming in 1024 byte per chunk

    # Option 1
    progress = tqdm(total=file_size, unit='B', unit_scale=True, desc="Downloading {file}".format(file=file_name))

    # Open the file destination and write in binary mode
    with open(together, "wb") as f:
        # Loop through each of the chunks in response in chunk_size and update the progres by calling update using
        # len(chunk) not chunk_size
        for chunk in response.iter_content(chunk_size):
            f.write(chunk)

            progress.update(len(chunk))

    # Option 2
    """progress = tqdm(response.iter_content(chunk_size),total=file_size, unit='B', unit_scale=True, desc="Downloading {file}".format(file = file_name))

    with open(together, "wb") as f:

        for chunk in progress:

            progress.update(len(chunk))
            f.write(chunk)

    # Close the tqdm object and file object as good practice
    """

    progress.close()
    f.close()


if __name__ == "__main__":
    download_image("Image Link")

看起来像是 tqdm 的现有错误。 https://github.com/tqdm/tqdm/issues/766

选项 1:

  • 提供 tqdm 的总大小
  • 在每次迭代中,更新进度。期待进度条继续移动。
  • 工作正常。

选项 2:

  • 为 tqdm 提供总大小以及跟踪进度的 generator 函数。
  • 在每次迭代中,它应该自动从生成器获取更新并推送进度条。
  • 不过,你也手动调用了progress.update应该不是这样的。
  • 而是让生成器来完成这项工作。
  • 但这也不起作用,并且已经报告了该问题。

关于选项 1 的建议: 为避免手动关闭流,您可以将它们包含在 with 语句中。这同样适用于 tqdm。

# Open the file destination and write in binary mode
    with tqdm(total=file_size, 
        unit='B', 
        unit_scale=True, 
        desc="Downloading {file}".format(file=file_name)
    ) as progress, open(file_name, "wb") as f:
        # Loop through each of the chunks in response in chunk_size and update the progres by calling update using
        # len(chunk) not chunk_size
        for chunk in response.iter_content(chunk_size):
            progress.update(len(chunk))
            f.write(chunk)