1

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'
linuxfan
  • 1,020
  • 12
  • 26
  • I'm not seeing anything based on either the `threading` nor the `multiprocessing` libraries. Why don't you include the portion of your code involving either of those (presumably using a timeout)? – BlackVegetable Mar 25 '15 at 02:49
  • If you are on posix platform, you could also use `signal` – wim Mar 25 '15 at 02:49
  • if your not on windows, have you tried using select, as metioned here - http://stackoverflow.com/questions/3471461/raw-input-and-timeout – Calum Mar 25 '15 at 02:51
  • I'm running on linux - thanks for the pointer. Will lookup the usage of signal. – linuxfan Mar 25 '15 at 02:52
  • Python Module of the Week has a post on [timeout with multiprocessing](http://pymotw.com/2/multiprocessing/basics.html#waiting-for-processes) – LinkBerest Mar 25 '15 at 02:53

0 Answers0