Need function like "raw_input", but with time limit
Fredrik Lundh
fredrik at pythonware.com
Sun Sep 19 06:00:09 EDT 2004
"Radioactive Man" <rm at rm.rm> wrote:
> anyone know of a function like "raw_input", which collects a string
> from the user entry, but one where I can set a time limit, as follows:
>
> time_limit = 10 # seconds
> user_answer = function_xyz("GIVE ME AN ANSWER: ", time_limit)
this works on some platforms:
import signal, sys
def alarm_handler(*args):
raise Exception("timeout")
def function_xyz(prompt, timeout):
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(timeout)
sys.stdout.write(prompt)
sys.stdout.flush()
try:
text = sys.stdin.readline()
except:
text = ""
signal.alarm(0)
return text
</F>
More information about the Python-list
mailing list