timeout using alarm signal question??

Ishwar Rattan rattan at cps.cmich.edu
Thu Jul 22 17:49:27 CEST 2004


System is Mandrale 9.1 Linux with Pyhton-2.2.3

I want to abort the stdin-read operation if no input is avalable. Is it possible
to achieve this?

Sample code is given below.

-ishwar
---
import sys, signal, time
atry = 0

def now():
   return time.ctime(time.time())

def onSignal(signum, stackframe):
   global atry
   if signum == signal.SIGALRM:
        print 'alarm signal seen..', atry

def main():
        global atry
        while atry < 3:
           signal.signal(signal.SIGALRM, onSignal)
           signal.alarm(1)
           resp = raw_input('>> ')
           if len(resp) > 0:         # cancel alaram input is available
                   signal.alarm(0)
                   break
           else:                     # try again!
                   atry = atry + 1

if __name__ == '__main__':
        main()
---



More information about the Python-list mailing list