小编jus*_*ker的帖子

Python中select()之间的键盘输入

我写了一些代码来从键盘获取输入,并检查是否有效:

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
Run Code Online (Sandbox Code Playgroud)

我发现当键盘输入在等待之外select()结束时(通常在5秒内结束is_alive()),if rlist:将会出现错误.

我能理解为什么,但我不知道如何解决它.

还有另一个与上述情况有关的问题,readline()当某些输入位于不同的select()等待时,有时会返回我输入的最后一行.

这意味着,如果我输入'abc \n'并且不幸的是'\n'位于wating之外select()(这意味着,当我按Enter时,程序正在执行其他部分,例如is_alive()),然后如果我输入 …

python select stdin readline keyboard-input

5
推荐指数
1
解决办法
1570
查看次数

标签 统计

keyboard-input ×1

python ×1

readline ×1

select ×1

stdin ×1