0

我有以下 urwid 程序,它显示按下的键,或来自 Popen'd 程序的任何行:

#!/usr/bin/env python
import urwid
from threading import Thread
from subprocess import Popen, PIPE
import time
import os


class MyText(urwid.Text):
    def __init__(self):
        super(MyText, self).__init__('Press Q to quit', align='center')

    def selectable(self):
        return True

    def keypress(self, size, key):
        if key in ['q', 'Q']:
            raise urwid.ExitMainLoop()
        else:
            self.set_text(repr(key))


class Writer(object):
    def __init__(self):
        self._child = Popen(
                'for i in `seq 5`; do sleep 1; echo $i; done',
                #"ssh localhost 'for i in `seq 5`; do sleep 1; echo $i; done'",
                shell=True, stdout=PIPE, stderr=PIPE)

    def file(self):
        return self._child.stdout

    def fileno(self):
        return self._child.stdout.fileno()

w = Writer()
txt = MyText()
top = urwid.Filler(txt)
mainloop = urwid.MainLoop(top)

def on_writer():
    c = w.file().read(1)
    if c == '': # terminated
        mainloop.remove_watch_file(w.fileno())
        return
    if c == '\n':
        return
    txt.set_text(c)
    mainloop.draw_screen()

mainloop.watch_file(w.fileno(), on_writer)

mainloop.run()

上面的程序可以工作,但是如果我将 Popen'd 命令更改为ssh localhost ...版本,程序将停止显示按键,直到ssh localhost ...命令完成。这是为什么?

环境:CentOS 6.6,Python 2.7.4,urwid 1.3.1-dev。

4

1 回答 1

0

问题是 ssh 试图操作它的stdin. 将其设置stdin为虚拟文件描述符可以修复它。

class Writer(object):
    def __init__(self):
        r, w = os.pipe()
        self._child = Popen(
                #'for i in `seq 5`; do sleep 1; echo $i; done',
                "ssh localhost 'for i in `seq 5`; do sleep 1; echo $i; done'",
                shell=True, stdin=r, stdout=PIPE, stderr=PIPE)
        os.close(w)
于 2014-11-19T17:55:55.293 回答