通过关闭屏蔽功能,您一次只能读取一个字符。因此,无法readline()
在非阻塞上下文中工作。我假设您只是想阅读按键来控制机器人。
我select.select()
在Linux上没有运气,并创建了一种调整termios
设置的方法。因此,这是特定于Linux的,但对我有用:
old_settings=None
def init_anykey():
global old_settings
old_settings = termios.tcgetattr(sys.stdin)
new_settings = termios.tcgetattr(sys.stdin)
new_settings[3] = new_settings[3] & ~(termios.ECHO | termios.ICANON) # lflags
new_settings[6][termios.VMIN] = 0 # cc
new_settings[6][termios.VTIME] = 0 # cc
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, new_settings)
@atexit.register
def term_anykey():
global old_settings
if old_settings:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
def anykey():
ch_set = []
ch = os.read(sys.stdin.fileno(), 1)
while ch != None and len(ch) > 0:
ch_set.append( ord(ch[0]) )
ch = os.read(sys.stdin.fileno(), 1)
return ch_set;
init_anykey()
while True:
key = anykey()
if key != None:
print key
else:
time.sleep(0.1)
更好的Windows或跨平台答案在这里:Python非阻塞控制台输入