5

I write some codes to get the input from keyboard and also check something is alive or not:

import sys
from select import select

timeout = 10
while is_alive(): # is_alive is a method to check some stuffs, might take 5 secs
    rlist, _, _ = select([sys.stdin], [], [], timeout)
    if rlist:
        s = sys.stdin.readline()
        print repr(s)
        handle(s) # handle is a method to handle and react according to input s

I found that when the keyboard input ends outside of the waiting in select() (usually it ends during the 5 secs of is_alive()), the if rlist: will get false.

I can understand why but I don't know how to solve it.

And there is still another question related to the situation mentioned above, sometimes readline() will return the last line of my input when some inputs are located across different select() waiting.

That means, if I enter 'abc\n' and unfortunately the '\n' located outside of wating in select() (that means, when I press Enter, the program are executing other parts, such as is_alive()), and then if I enter 'def\n' and this time the Enter pressed successfully located within select(), I'll see the s from readline() becomes 'def\n' and the first line is disappeared.

Is there any good solution to solve two issues above? I'm using FreeBSD 9.0.

4

1 回答 1

2

正如您在is_alive()调用中的代码ssh,这将消耗标准输入。

尝试从ssh选项-n或重新定向的stdin.

后者将与

sp = subprocess.Popen(..., stdin=subprocess.PIPE)
sp.stdin.close()
于 2012-12-06T08:44:24.303 回答