Time out question

Grant Edwards grante at visi.com
Mon Jul 3 10:30:10 EDT 2006


On 2006-07-03, Nick Craig-Wood <nick at craig-wood.com> wrote:
> Alex Martelli <aleax at mac.com> wrote:
>>  DarkBlue <nomail at nixmail.com> wrote:
>> >  try for 10 seconds
>> >    if database.connected :
>> >     do your remote thing
>> >  except raise after 10 seconds
>> >    abort any connection attempt
>> >    do something else 
>> 
>>  Sure, see function setdefaulttimeout in module socket.  Just call it as
>>  you wish before trying the DB connection and catch the resulting
>>  exception.
>
> It would be nice to have language support for the general case, ie a
> general purpose timeout.

I just use signal.alarm():

import signal,sys

def alarmHandler(signum, frame):
    raise 'Timeout'

signal.signal(signal.SIGALRM, alarmHandler)

while 1:
    try:
        signal.alarm(5)
        t = sys.stdin.readline()
        signal.alarm(0)
        print t
    except 'Timeout':
        print "too slow"

-- 
Grant Edwards                   grante             Yow!  I will SHAVE and
                                  at               buy JELL-O and bring my
                               visi.com            MARRIAGE MANUAL!!



More information about the Python-list mailing list