I have a simple menu that presents a few choices to a user. If the user doesn't enter his choice in, say, 2 minutes, I want to quit the program. Since execution is blocked at raw_input
, I see that a solution using either threading
and multiprocessing
is required, wherein a method runs every N seconds
and computes the difference between the current time and the last time a choice was made. I tried a few things based on examples found on SO and elsewhere, but couldn't come up with anything concrete. Hence I'm including just the sekelton of my menu code here. Any pointers will be appreciated.
#!/usr/bin/env python
def print_menu():
print 'Choice 1) Running'
print 'Choice 2) Walking'
print 'Choice 3) Swimming'
print
while True:
print_menu()
try:
inp = raw_input('Enter -> ')
except EOFError:
print 'Bad input.. Continue'