I have the following urwid program that displays the key pressed, or any line that comes in from the Popen'd program:
#!/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()
The above program works, but if I change the Popen'd command to the ssh localhost ...
version, the program stops displaying keypresses until the ssh localhost ...
command finishes. Why is that?
Environment: CentOS 6.6, Python 2.7.4, urwid 1.3.1-dev.
The problem is that ssh tries to operate its
stdin
. Setting itsstdin
to a dummy file descriptor fixes it.