Need function like "raw_input", but with time limit

Radioactive Man rm at rm.rm
Mon Sep 20 03:47:00 CEST 2004


On Sun, 19 Sep 2004 12:00:09 +0200, "Fredrik Lundh"
<fredrik at pythonware.com> wrote:

>"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> 
>
>

Is that for a Unix system?  I am running windows 95 and/or XP and my
signal.signal module does not have a "SIGALRM" attribute.  Thus, I get
an error message when I try to run that script.




More information about the Python-list mailing list