NOTES: Running Python 2.7, working on a macbook (OSX)
I want to query my user for an input, but if no input occurs after a time interval, run something else and try again. Here's what I have written:
import sys, select
def test():
waittime = 4
i, o, e = select.select( [sys.stdin], [], [], waittime)
if i:
print 'got i'
else:
print 'did not get i'
test()
What this DOES: If user does not press [return], waits 3 seconds, prints 'did not get i', reruns the function. If user presses [return], if (i) statement runs indefinitely.
What I WANT it do do: If the user presses [return], print 'got i', rerun the function, WAIT FOR USER TO PRESS RETURN. If user does not press return, wait three seconds, print 'did not get i', try again.
Thanks in advance for any help! -Erik
First I solved that with multithreading, but I guess it was overboard.
import sys,time,select
anidle = 0.0
while True:
time.sleep(0.01)
incoming = select.select([sys.stdin],[],[],0.0)[0]
if len(incoming) > 0:
anidle = 0.0
aline = sys.stdin.readline()
# process the input here
print 'Input:', aline
break
anidle += 0.01
if anidle > 4:
# process no input for 4 seconds here
print 'No input.'
break