您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

从raw_input()读取输入,而提示没有被Python中的其他线程覆盖

从raw_input()读取输入,而提示没有被Python中的其他线程覆盖

我最近遇到了这个问题,并希望将此解决方案留在这里以备将来参考。这些解决方案从终端清除待处理的raw_input(readline)文本,打印新文本,然后将raw_input缓冲区中的内容重新打印到终端。

一个程序非常简单,但是仅在只有一行文本等待raw_input时才能正确运行:

#!/usr/bin/python

import time,readline,thread,sys

def noisy_thread():
    while True:
        time.sleep(3)
        sys.stdout.write('\r'+' '*(len(readline.get_line_buffer())+2)+'\r')
        print 'Interrupting text!'
        sys.stdout.write('> ' + readline.get_line_buffer())
        sys.stdout.flush()

thread.start_new_thread(noisy_thread, ())
while True:
    s = raw_input('> ')

输出

$ ./threads_input.py
Interrupting text!
Interrupting text!
Interrupting text!
> WELL, PRINCE, Genoa and Lucca are Now no more than private estates of the Bo
Interrupting text!
> WELL, PRINCE, Genoa and Lucca are Now no more than private estates of the Bo
naparte family. No, I warn you, that if you do not tell me we are at war,

第二个可以正确处理2条或更多的缓冲行,但是具有更多(标准)模块依赖性,并且需要一点点终端黑客:

#!/usr/bin/python

import time,readline,thread
import sys,struct,fcntl,termios

def blank_current_readline():
    # Next line said to be reasonably portable for varIoUs Unixes
    (rows,cols) = struct.unpack('hh', fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ,'1234'))

    text_len = len(readline.get_line_buffer())+2

    # ANSI escape sequences (All VT100 except ESC[0G)
    sys.stdout.write('\x1b[2K')                         # Clear current line
    sys.stdout.write('\x1b[1A\x1b[2K'*(text_len/cols))  # Move cursor up and clear line
    sys.stdout.write('\x1b[0G')                         # Move to start of line


def noisy_thread():
    while True:
        time.sleep(3)
        blank_current_readline()
        print 'Interrupting text!'
        sys.stdout.write('> ' + readline.get_line_buffer())
        sys.stdout.flush()          # Needed or text doesn't show until a key is pressed


if __name__ == '__main__':
    thread.start_new_thread(noisy_thread, ())
    while True:
        s = raw_input('> ')

输出。先前的readline行已正确清除:

$ ./threads_input2.py
Interrupting text!
Interrupting text!
Interrupting text!
Interrupting text!
> WELL, PRINCE, Genoa and Lucca are Now no more than private estates of the Bo
naparte family. No, I warn you, that if you do not tell me we are at war,
python 2022/1/1 18:39:52 有448人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶