I am writing several codes to get keyboard input, and also to check if 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 keyboard input ends outside of the wait in select() (usually it ends within 5 seconds is_alive() ), if rlist: will get false.
I can understand why, but I donβt know how to solve it.
And there is another question related to the situation mentioned above, sometimes readline() will return the last line of my input when some inputs are in different select() expectations.
This means that if I type 'abc \ n' and, unfortunately, '\ n' located outside of wating in select() (this means when I press Enter , the program executes other parts such as is_alive() ) and then if I type 'def \ n' , and this time Enter pressed successfully, located inside select() , I will see s from readline() will become 'def \ n' , and the first line will disappear.
Is there a good solution to solving the two above issues? I am using FreeBSD 9.0.
source share