Timeout at command prompt

Tim Golden tim.golden at viacom-outdoor.co.uk
Thu Jan 12 16:28:42 CET 2006


[Thierry Lam]

[to do with a Win32 equivalent to the SIGALRM
interruption of a raw_input]

| Is there a windows equivalent for that solution?

Nothing so straightforward. Depends how hard you want 
to try. A couple of past threads on pretty much the 
exact same issue offer no equivalent solution. 

Any solution which I can come with involves some
sort of getch loop with more-or-less manual timeout.
Haven't examined the source for raw_input code, but
I bet it calls the underlying c-lib get-a-string routine
which probably doesn't allow for any interrupts.

Something like the following (crude but workable):

<code>
import msvcrt
import time

TIMEOUT_SECS = 5.0

line = ""
print "Prompt> ",
t0 = time.time ()
try:

  while True:
    if time.time () - t0 > TIMEOUT_SECS:
      raise KeyboardInterrupt
  
    if msvcrt.kbhit ():
      k = msvcrt.getch ()
      if k == "\r":
        break
  
      msvcrt.putch (k)
      line += k

except KeyboardInterrupt:
  print
  print "Interrupted"
else:
  print
  print "String was:", line

</code>

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list