推荐学习书目
› 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
vftj
V2EX  ›  Python

如何用 Python 实现类似 Linux 的 watch 功能并且保证刷新时不闪烁

  •  
  •   vftj · Dec 20, 2022 · 3886 views
    This topic created in 1375 days ago, the information mentioned may be changed or developed.

    现在的实现思路是:

    import os, sys, time
    while True:
        output = str(time.time())
        os.system('clear')
        sys.stdout.write(output)
        sys.stdout.flush()
        time.sleep(1)
    

    但发现每次刷新的时候都会闪一下,而 watch 是不会的,求教有办法改进吗

    2 replies  •  2022-12-20 11:40:51 +08:00
    ysc3839
        1
    ysc3839  
       Dec 20, 2022   ❤️ 3
    可以看看 watch 的源代码,比如 busybox 的 watch https://git.busybox.net/busybox/tree/procps/watch.c 就只是 printf(ESC"[H" ESC"[J");
    而 procps-ng 的 watch 则复杂很多 https://gitlab.com/procps-ng/procps/-/blob/master/src/watch.c
    vftj
        2
    vftj  
    OP
       Dec 20, 2022
    @ysc3839 非常感谢,我把 os.system('clear')改成了 print('\033[H\033[J')就不闪了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2523 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:33 · PVG 23:33 · LAX 08:33 · JFK 11:33
    ♥ Do have faith in what you're doing.