timeout using alarm signal question??

Discussion in 'Python' started by Ishwar Rattan, Jul 22, 2004.

  1. 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()
    ---
    Ishwar Rattan, Jul 22, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ishwar Rattan

    timeout on on socket using alarm signal??

    Ishwar Rattan, Jul 22, 2004, in forum: Python
    Replies:
    0
    Views:
    604
    Ishwar Rattan
    Jul 22, 2004
  2. Replies:
    0
    Views:
    321
  3. Jp Calderone
    Replies:
    1
    Views:
    318
  4. Adrian Casey

    Using signal.alarm to terminate a thread

    Adrian Casey, Nov 13, 2006, in forum: Python
    Replies:
    7
    Views:
    595
    Nick Craig-Wood
    Nov 15, 2006
  5. Prabh
    Replies:
    2
    Views:
    97
    Charles DeRykus
    Apr 25, 2004
Loading...

Share This Page