Python诅咒从另一个线程处理stdout

时间:2015-07-21 15:24:27

标签: python multithreading python-curses

我在我的python程序中运行两个线程,一个使用python curses运行菜单系统并等待输入的线程,一个线程根据菜单选项进行分析并输出它的状态通过内置的print()函数。我的问题是打印不能与curses一起使用,因为如果curses.echo()打开,那么它会打印到我等待输入的行,如果使用curses.noecho(),然后根本不显示输出。

由于我想控制显示输出的位置和时间,我最初的解决方案是设置window.timeout(1000),然后像这样输入循环:

try:
    c = window.getkey()
except:
    c = -1 #timeout or error in input

if c == -1:
    check_for_input()
elif c == 'KEY_RESIZE':
    ...

这非常有效,允许我每秒检查stdout的输出,然后如果需要更新菜单,同时仍然允许用户输入。我遇到的问题是我不知道如何捕获标准输出并选择在需要时显示它。这有可能吗?

1 个答案:

答案 0 :(得分:2)

所以我想出了这个,但作为免责声明,我不知道这是否是线程安全的(尽管到目前为止没有问题)。

可以使用python库io捕获打印输出,更具体地说,可以从该库中捕获StringIO

N.B。这适用于Python3

基本上,解决方案是将sys.stdout设置为io.StringIO的实例并从中读取。

external_output = None
stdout_buff = io.StringIO()
sys.stdout = stdout_buff
stream_pos = 0 # lst read position of the stdout stream.

while True: #input loop
    ...
    if stdout_buff.tell() > stream_pos:
        stdout_buff.seek(stream_pos)
        external_output = stdout_buff.read()
        stream_pos = stdout_buff.tell()
    ...

下面我列举了一个我正在使用的菜单系统的简短示例,以防上述任何有此问题的人都不清楚,希望能够清除它。

干杯!

未修改版本

所以菜单的显示和事件循环看起来很像这样:(请注意,这是事物的简化版本,因此与显示菜单和显示用户输入的内容有很大关系遗漏了)。此基本示例显示一个菜单,允许用户退出程序,在其选择中输入数字,或输入他们的选择,然后打印出来。

import sys
import curses

def menu(stdscr):
    # initial startup settings
    curses.start_color()
    curses.use_default_colors()
    stdscr.timeout(1000) #timeout the input loop every 1000 milliseconds
    user_selection = ''
    # other unrelated initial variables

    while True: #display loop
        stdscr.clear()
        # the following is actually in a function to handle automatically
        # taking care of fitting output to the screen and keeping
        # track of line numbers, etc. but for demonstration purposes
        # I'm using the this
        start_y = 0
        stdscr.addstr(start_y, 0, 'Menu Options:')
        stdscr.addstr(start_y+1, 0, '1) option 1')
        stdscr.addstr(start_y+2, 0, '1) option 2')
        stdscr.addstr(start_y+3, 0, '1) option 3')
        stdscr.addstr(start_y+4, 0, '1) option 4')

        while True: #input loop
            c = stdscr.getkey()
            if c == 'KEY_RESIZE':
                handle_window_resize() # handle changing stored widths and height of window
                break #break to redraw screen
            elif c.isdigit():
                # if user typed a digit, add that to the selection string
                # users may only select digits as their options
                user_selection += c 
            elif c == '\n':
                # user hit enter to submit their selection
                if len(user_selection) > 0:
                    return user_selection
            elif c == 'q':
                sys.exit()



result = curses.wrapper(menu)
print(result)

在此示例中,仍然会出现问题,即同时运行到此线程的线程的任何输出都将打印在程序当前正在等待用户输入的stdscr光标处。

修改版

import sys
import curses
from io import StringIO


def menu(stdscr):
    # initial startup settings
    curses.start_color()
    curses.use_default_colors()
    stdscr.timeout(1000) #timeout the input loop every 1000 milliseconds
    user_selection = ''
    # other unrelated initial variables

    # output handling variables
    external_output = None # latest output from stdout
    external_nlines = 2 # number of lines at top to leave for external output
    stdout_buff = StringIO()
    sys.stdout = stdout_buff
    stream_pos = 0 # lst read position of the stdout stream.

    while True: #display loop
        stdscr.clear()
        # the following is actually in a function to handle automatically
        # taking care of fitting output to the screen and keeping
        # track of line numbers, etc. but for demonstration purposes
        # I'm using the this
        if external_output is not None:
            stdscr.addstr(0, 0, "stdout: " + external_output)

        start_y = external_nlines
        stdscr.addstr(start_y, 0, 'Menu Options:')
        stdscr.addstr(start_y+1, 0, '1) option 1')
        stdscr.addstr(start_y+2, 0, '1) option 2')
        stdscr.addstr(start_y+3, 0, '1) option 3')
        stdscr.addstr(start_y+4, 0, '1) option 4')

        while True: #input loop
            try:
                c = stdscr.getkey()
            except:
                c = -1 # 1000ms timeout or error

            if c == -1:
                if stdout_buff.tell() > stream_pos:
                    # current stdout_buff pos is greater than last read
                    # stream position, so there is unread output
                    stdout_buff.seek(stream_pos)
                    external_output = stdout_buff.read().strip() #strip whitespace
                    stream_pos = stdout_buff.tell() #set stream_pos to end of stdout_buff
                    break #redraw screen with new output
            elif c == 'KEY_RESIZE':
                handle_window_resize() # handle changing stored widths and height of window
                break #break to redraw screen
            elif c.isdigit():
                # if user typed a digit, add that to the selection string
                # users may only select digits as their options
                user_selection += c 
            elif c == '\n':
                # user hit enter to submit their selection
                if len(user_selection) > 0:
                    sys.stdout = sys.__stdout__ # reset stdout to normal
                    return user_selection
            elif c == 'q':
                sys.stdout = sys.__stdout__ # reset stdout to normal
                sys.exit()



result = curses.wrapper(menu)
print(result)