Issue
Conflict with STDIN when executing these two functions. What would cause the second function to not read STDIN correctly? If the first function is not executed, the second function has no issue reading the input.
To clear STDIN buffer I've tried:
'sys.stdin.flush'
'tcflush(sys.stdin, TCIOFLUSH)'
Python Code
import sys, select
def stdin_read():
print "[stdin read] You must answer Y/N and press ENTER"
sys.stdout.flush()
response = sys.stdin.read(1)
print "You said '{}'".format(response)
def stdin_timeout():
print "[stdin timeout] You must answer YES / NO in 10 seconds and press ENTER"
sys.stdout.flush()
sys.stdin.flush()
i, o, e = select.select( [sys.stdin], [], [], 10 )
if (i):
print "You said '{}'".format(sys.stdin.readline().strip())
exit(0)
else:
print "You said nothing!"
exit(1)
stdin_read()
stdin_timeout()
Python Output Both Functions:
:~# python input.py
[stdin read] You must answer Y/N and press ENTER
n
You said 'n'
[stdin timeout] You must answer YES / NO in 10 seconds and press ENTER
no
You said ''
:~# no
-bash: no: command not found
Python Output Second Function
~# python input.py
[stdin timeout] You must answer YES / NO in 10 seconds and press ENTER
no
You said 'no'