使用Python中的raw_input()从输入中读取数据,而不被其他线程所覆盖。

2024-05-23 14:48:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图让用户使用raw_input()在控制台输入命令,这很好。问题是我有一些后台线程,它们偶尔会将日志信息输出到屏幕上,当它们这样做时,它们会弄乱输入提示(因为此时光标正好在哪里,输出就在哪里)。

这是一个小的Python程序,它说明了我的意思。

#!/usr/bin/env python
import threading
import time

def message_loop():
    while True:
        time.sleep(1)
        print "Hello World"

thread = threading.Thread(target = message_loop)
thread.start()

while True:
    input = raw_input("Prompt> ")
    print "You typed", input

这是我运行它时它的外观示例:

Prompt> Hello World
Hello World
Hello World
Hello World
test
You typed test
Prompt> Hello World
Hello World
Hello World
hellHello World
o
You typed hello
Prompt> Hello World
Hello World
Hello World
Hello World

我想要的是提示与线程的输出一起移动。就像这样:

Hello World
Hello World
Prompt> test
You typed test
Hello World
Hello World
Hello World
Hello World
Hello World
Prompt> hello
You typed hello
Hello World
Hello World
Hello World
Hello World
Prompt> 

有没有办法在不使用丑陋的黑客手段的情况下实现这一目标?:)


Tags: testimportloopyoumessagehelloworldinput
3条回答

您需要从一个线程而不是多个线程更新stdout。。。否则您无法控制交叉i/o

您需要创建一个用于输出写入的线程。

您可以在线程中使用队列,并让所有其他线程将其输出日志信息写入该队列。。然后从这个队列中读取并在适当的时间将提示消息写入stdout。

我认为您需要一些可以从终端窗口动态打印/删除/覆盖文本的东西,例如UNIX watchtop命令是如何工作的。

我认为在您的情况下,您可以打印“Prompt>;”,但当您得到“Hello World”时,您会用“Hello World”覆盖“Prompt>;”,然后在下面的行中打印“Prompt>;”。我不认为你能做到定期输出打印到终端。

您也许可以使用Python的curses库来做您想要的事情。我从来没有用过它,所以我不能告诉你如何解决你的问题(或者模块是否能够解决你的问题),但我认为这是值得一看的。搜索“python curses tutorial”提供了一个PDF tutorial document似乎很有用。

我最近遇到了这个问题,希望把这个解决方案留在这里供以后参考。 这些解决方案从终端清除挂起的原始输入(readline)文本,打印新文本,然后将原始输入缓冲区中的内容重新打印到终端。

第一个程序非常简单,但只有在只有一行文本等待原始输入时才能正常工作:

#!/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('> ')

输出。正确清除以前的阅读行:

$ ./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,

有用的来源:

How to get Linux console window width in Python

apt like column output - python library (此代码示例演示如何获取Unix或Windows的终端宽度)

http://en.wikipedia.org/wiki/ANSI_escape_code

相关问题 更多 >