惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
有赞技术团队
有赞技术团队
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
The Cloudflare Blog
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Vercel News
Vercel News
IT之家
IT之家
MyScale Blog
MyScale Blog
博客园_首页
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
罗磊的独立博客

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Tqdm Issues and Tips
2020-09-05 · via jdhao's digital space

Tqdm is a popular package to show a progress bar. Here are a few tips when using tqdm.

Windows display issue#

On Windows, it seems that tqdm will print a new line when showing the progress, which is annoying.

According to issue here, this is due to buggy Unicode handling in Windows terminal. We can add ascii=True in tqdm() to prevent this behavior. Works for me on Windows for both Cmder and plain cmd.

Iterate over each line in a file and show progress?#

When we are processing files with large number of lines, we may want to see the progress. To see the progress, we need to know the number of lines in this file. Here is a sample code to show the progress bar using tqdm:

from tqdm import tqdm

file_path = "test.txt"
num_lines = sum(1 for line in open(file_path, 'r'))

with open(file_path, 'r') as f:
    for line in tqdm(f, total=num_lines):
        # deal with this line...
        pass

Ref

Show progress for concurrent.futures.ThreadPoolExecutor#

Here is an example of using tqdm together with concurrent.futures:

from concurrent.futures import ThreadPoolExecutor
import time

from tqdm import tqdm


def square(x):
    time.sleep(0.01)
    return x*x


def main():
    nums = list(range(10000))
    with ThreadPoolExecutor() as executor:
        results = list(tqdm(executor.map(square, nums), total=len(nums)))

if __name__ == "__main__":
    main()

The output of executor.map(square, nums) is a generator containing all the returned results from the function square(). So we need to supply the total number in the total argument.

In the latest version of tqdm, we can also simplify the above code by using tqdm.contrib.concurrent.thread_map() method, which is essentially a wrapper around above code:

from concurrent.futures import ThreadPoolExecutor
import time

from tqdm.contrib.concurrent import thread_map


def square(x):
    time.sleep(0.01)
    return x*x


def main():
    nums = list(range(10000))
    with ThreadPoolExecutor() as executor:
        results = thread_map(square, nums)

if __name__ == "__main__":
    main()

The above code is tested on tqdm version 4.48.2.

Ref: