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
vftj
V2EX  ›  Python

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

  •  
  •   vftj · 2022-12-20 10:40:26 +08:00 · 2520 次点击
    这是一个创建于 464 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现在的实现思路是:

    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 条回复    2022-12-20 11:40:51 +08:00
    ysc3839
        1
    ysc3839  
       2022-12-20 10:56:06 +08:00   ❤️ 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
       2022-12-20 11:40:51 +08:00
    @ysc3839 非常感谢,我把 os.system('clear')改成了 print('\033[H\033[J')就不闪了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2981 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 15:05 · PVG 23:05 · LAX 08:05 · JFK 11:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.