Python urwid watch_文件块按键
我有以下urwid程序,显示按下的键,或从Popen'd程序输入的任何行:Python urwid watch_文件块按键,python,urwid,Python,Urwid,我有以下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__('P
#!/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.问题是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)