Thread: Timed Inputs??
View Single Post
Old Jun 27th, 2005, 10:36 AM   #7
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Meh, select.poll is *nix only as well. Oh well.. here's the same example using select.select instead of select.poll (which, unless i'm grossly mistaken, is cross platform ). The reason I didn't use it to begin with is that select.poll is more readable.
import sys, select

def readWithTimeout(message, timeout):
    """ Like raw_input, but you specify a timeout (in seconds). If that time
    is reached, then this just returns a blank string. """
    output = select.select([sys.stdin], [], [], timeout)
    if len(output[0]):
        return sys.stdin.readline()
    else:
        return ""

# Timeouts after 5 seconds.
name = readWithTimeout("Yeah, your name like: ", 5)
Cerulean is offline   Reply With Quote