【问题标题】:Python curses handling stdout from another threadPython 诅咒从另一个线程处理标准输出
【发布时间】:2015-07-21 15:24:26
【问题描述】:

我在我的 python 程序中运行两个线程,一个线程使用 python curses 运行菜单系统并等待输入,一个线程基于菜单选择进行分析并通过内置 @987654321 输出其状态@ 功能。我的问题是,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':
    ...

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

【问题讨论】:

    标签: python multithreading python-curses


    【解决方案1】:

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

    可以使用 python 库 io,更具体地说是来自该库的 StringIO 来捕获 print 的输出。

    注意这是针对 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)
    

    【讨论】:

      猜你喜欢
      • 2018-12-21
      • 1970-01-01
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多