推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
Morriaty
V2EX  ›  Python

为啥 tqdm 已经落地的历史日志,使用 tail 查看时依然会把进程从 0 到 100 再打印一遍?

  •  
  •   Morriaty · Aug 1, 2023 · 2405 views
    This topic created in 1162 days ago, the information mentioned may be changed or developed.

    代码里的调用

    for i in tqdm(generator(), total=gr.size, desc="writing"):
    

    然后在 shell 里再重定向到log_file

    python3 script.py >> log_file 2>&1
    

    这是个 cron 定时脚本,第二天等这个脚本执行结束后,用 tail 去查看

    tail -100 log_file
    

    然后发现竟然会把进程条从 0 到 100 再打印一遍,这是为啥呢? 就是它为啥不是直接显示的 100%进度条呢?

    补充说明下: 它不是梯形打印

    writing: 10%|█                  | 2230000/2430799 [1:22:45<5:22:45, 125.52it/s]
    writing: 30%|██              | 6230000/2430799 [3:22:45<5:22:45, 125.52it/s]
    writing: 50%|███           | 10230000/2430799 [3:22:45<5:22:45, 125.52it/s]
    

    而是在单行里一直从 0 打印到 100

    5 replies  •  2023-08-01 11:44:57 +08:00
    thinszx
        1
    thinszx  
       Aug 1, 2023
    输出是不是没有换行符?可能文件里都是一行?
    Morriaty
        2
    Morriaty  
    OP
       Aug 1, 2023
    @thinszx 是一行,但我的问题是,为什么不是只直接打印 100%这一行的结果,而实际情况是它在一行里把 0%-100%这个过程又重复了一遍
    thinkershare
        3
    thinkershare  
       Aug 1, 2023   ❤️ 1
    tqdm 不适合重定向,但对输出做了刷新,重定向收到网络和 I/O 的影响,会出现各种乱七八糟的结果。
    cbythe434
        4
    cbythe434  
       Aug 1, 2023   ❤️ 2
    tqdm 输出的是\r
    换行(\r ):每次光标移到本行的行首位置处
    tail 是看\n 的,必须所有\r 刷完
    0d
        5
    0d  
       Aug 1, 2023 via iPhone   ❤️ 1
    终端的动态效果都是由控制字符实现的

    可以看看这篇博客
    https://poor.dev/blog/terminal-anatomy/
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1397 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 16:18 · PVG 00:18 · LAX 09:18 · JFK 12:18
    ♥ Do have faith in what you're doing.