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

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