By turning blocking off you can only read a character at a time. So, there is no way to get readline()
to work in a non-blocking context. I assume you just want to read key presses to control the robot.
I have had no luck using select.select()
on Linux and created a way with tweaking termios
settings. So, this is Linux specific but works for me:
import atexit, termios
import sys, os
import time
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)
A better Windows or cross-platform answer is here: Non-blocking console input?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…