V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 270 天前 · 993 次点击
    这是一个创建于 270 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码里的调用

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

    可以看看这篇博客
    https://poor.dev/blog/terminal-anatomy/
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   906 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 22:16 · PVG 06:16 · LAX 15:16 · JFK 18:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.